LeetCode 231. Power of Two

LeetCode 第 231 題:"Power of Two",相當單純的題目。

Given an integer, write a function to determine if it is a power of two.

在之前做過 LeetCode 326 的 Power of Three, 我使用對數來處理,該實現代碼可以直接套用在 Power of Two 的需求上,但本文打算用 bits 來驗證該整數是否為 2 的次方數。

LeetCode 231. Power of Two_第1张图片
Power of Three, implemented by Log()

Step 1: n 為 0 或 負數,應回傳 false

測試代碼:

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void n_is_less_or_equal_0_should_return_false()
        {
            var n = 0;
            Assert.IsFalse(new Solution().IsPowerOfTwo(n));
        }
    }

通過測試的生產代碼:

    public class Solution
    {
        public bool IsPowerOfTwo(int n)
        {
            if (n <= 0) return false;
            throw new NotImplementedException();
        }
    }

重構 assertion:

LeetCode 231. Power of Two_第2张图片
refactor testing: extract method ShouldBeFalse()

Step 2: n 為 1,應為 2 的次方

測試代碼:

LeetCode 231. Power of Two_第3张图片
n is 1, should be power of two

生產代碼,hard-code 通過 n = 1 回傳 true。

LeetCode 231. Power of Two_第4张图片
hard-code, when n is 1, return true

重構 assertion 的部分:

LeetCode 231. Power of Two_第5张图片
refactor testing: extract method ShouldBeTrue()

Step 3: n 為 2, 應為 2 的次方

測試代碼:

        [TestMethod]
        public void n_is_2_should_return_true()
        {
            ShouldBeTrue(2);
        }

生產代碼:先用 mod 2 於 0 通過此測試案例。(hard-code 通過 test case 的情況)

LeetCode 231. Power of Two_第6张图片
pass test case by mod 2

Step 4: n 為 6, 為 2 的倍數,但非 2 的次方數

測試代碼:

LeetCode 231. Power of Two_第7张图片
n is 6, is not power of two

生產代碼:將 n 轉成 2 進位,當 1 的數量只有一個時,代表為 2 的次方數。

LeetCode 231. Power of Two_第8张图片
convert integer to binary and should only one "1"

重構:移除 n == 1 的判斷,因為 n == 1 的商業邏輯被包含在剛剛實現的代碼中。

LeetCode 231. Power of Two_第9张图片
refactor: remove n == 1 condition

Step 5: 重構,rename assertion function

將原本不具語意的 ShouldBeTrue()ShouldBeFalse() rename 為 IsPowerOfTwo()IsNotPowerOfTwo()

LeetCode 231. Power of Two_第10张图片
refactor testing: rename function more meaningful

通過 LeetCode 所有測試案例

LeetCode 231. Power of Two_第11张图片
Pass all test cases from LeetCode

摘要整理

  1. integer 轉成 2 進位字串,可用 Convert.ToString(n, 2)
  2. 次方數代表只有存在單一個 "1"
  3. 不要因為是測試程式或只是練習,而忽略了語意。語意的層次在解釋說明意義,而非說明實現過程的細節。ShouldBeTrue() 就是實現細節。IsPowerOfTwo() 才是解釋意圖。(同理,測試案例名稱也應該修改,不該用 should_be_true 而改採用 should_be_power_of_two

GitHub Commit History: LeetCode 231. Power of Two

你可能感兴趣的:(LeetCode 231. Power of Two)