From 0d565cca88aeb624d1a13d3a7375f5fbcfe3c910 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 21:49:14 +0530 Subject: [PATCH 01/12] added power_of_4 --- bit_manipulation/power_of_2.py | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 bit_manipulation/power_of_2.py diff --git a/bit_manipulation/power_of_2.py b/bit_manipulation/power_of_2.py new file mode 100644 index 000000000000..4478457a9d5f --- /dev/null +++ b/bit_manipulation/power_of_2.py @@ -0,0 +1,65 @@ +""" + +Task: +Given a positive int number. Return True if this number is power of 4 +or False otherwise. + +Implementation notes: Use bit manipulation. +For example if the number is the power of 2 it's bits representation: +n = 0..100..00 +n - 1 = 0..011..11 + +n & (n - 1) - no intersections = 0 +If the number is a power of 4 then it should be a power of 2 and the set bit should be at an odd position +""" + + +def power_of_4(number: int) -> bool: + """ + Return True if this number is power of 4 or False otherwise. + + >>> power_of_4(0) + Traceback (most recent call last): + ... + ValueError: number must be positive + >>> power_of_4(1) + True + >>> power_of_4(2) + False + >>> power_of_4(4) + True + >>> power_of_4(6) + False + >>> power_of_4(8) + False + >>> power_of_4(17) + False + >>> power_of_4(-1) + Traceback (most recent call last): + ... + ValueError: number must be positive + >>> power_of_4(1.2) + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for &: 'float' and 'float' + + """ + if number <= 0: + raise ValueError("number must be positive") + if number & (number - 1) == 0: + c = 0 + while number: + c+=1 + number>>=1 + if c&1: + return True + else: + return False + else: + return False + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 4722bb28199d09fd2688b03ffaa643e4af29c863 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 21:51:15 +0530 Subject: [PATCH 02/12] updated power_of_4 --- bit_manipulation/{power_of_2.py => power_of_4.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bit_manipulation/{power_of_2.py => power_of_4.py} (100%) diff --git a/bit_manipulation/power_of_2.py b/bit_manipulation/power_of_4.py similarity index 100% rename from bit_manipulation/power_of_2.py rename to bit_manipulation/power_of_4.py From 92b9420ee879a7190b6024bbac336fdcea91cf01 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 16:23:18 +0000 Subject: [PATCH 03/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/power_of_4.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/power_of_4.py b/bit_manipulation/power_of_4.py index 4478457a9d5f..bcd8d7aaecf6 100644 --- a/bit_manipulation/power_of_4.py +++ b/bit_manipulation/power_of_4.py @@ -49,9 +49,9 @@ def power_of_4(number: int) -> bool: if number & (number - 1) == 0: c = 0 while number: - c+=1 - number>>=1 - if c&1: + c += 1 + number >>= 1 + if c & 1: return True else: return False From 63ee5d85c574df4fd755b9d7f005eaf749d51201 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 22:00:02 +0530 Subject: [PATCH 04/12] updated power_of_4 --- bit_manipulation/power_of_4.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/power_of_4.py b/bit_manipulation/power_of_4.py index bcd8d7aaecf6..e9c98395b5cc 100644 --- a/bit_manipulation/power_of_4.py +++ b/bit_manipulation/power_of_4.py @@ -49,9 +49,9 @@ def power_of_4(number: int) -> bool: if number & (number - 1) == 0: c = 0 while number: - c += 1 - number >>= 1 - if c & 1: + c+=1 + number>>=1 + if c%2: return True else: return False From 1a9683e8cd1436f8192f0a25cdba6ef81bf8e32f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 16:30:53 +0000 Subject: [PATCH 05/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/power_of_4.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/power_of_4.py b/bit_manipulation/power_of_4.py index e9c98395b5cc..336c6d1a89d8 100644 --- a/bit_manipulation/power_of_4.py +++ b/bit_manipulation/power_of_4.py @@ -49,9 +49,9 @@ def power_of_4(number: int) -> bool: if number & (number - 1) == 0: c = 0 while number: - c+=1 - number>>=1 - if c%2: + c += 1 + number >>= 1 + if c % 2: return True else: return False From d7950c06f5443a2ada6253c792830166e1a2be41 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 22:09:02 +0530 Subject: [PATCH 06/12] updated power_of_4 --- bit_manipulation/power_of_4.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bit_manipulation/power_of_4.py b/bit_manipulation/power_of_4.py index 336c6d1a89d8..9365209c14da 100644 --- a/bit_manipulation/power_of_4.py +++ b/bit_manipulation/power_of_4.py @@ -51,10 +51,7 @@ def power_of_4(number: int) -> bool: while number: c += 1 number >>= 1 - if c % 2: - return True - else: - return False + return c%2 else: return False From 4f4d6dc846c13224440396114e9aaa7e32ef0824 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 16:39:49 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/power_of_4.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/power_of_4.py b/bit_manipulation/power_of_4.py index 9365209c14da..52e7049ac16b 100644 --- a/bit_manipulation/power_of_4.py +++ b/bit_manipulation/power_of_4.py @@ -51,7 +51,7 @@ def power_of_4(number: int) -> bool: while number: c += 1 number >>= 1 - return c%2 + return c % 2 else: return False From 079e7a0413c42f7914b6c5bce0f23a7629269788 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 22:20:06 +0530 Subject: [PATCH 08/12] updated power_of_4 --- bit_manipulation/power_of_4.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/power_of_4.py b/bit_manipulation/power_of_4.py index 52e7049ac16b..1777f8fe3f15 100644 --- a/bit_manipulation/power_of_4.py +++ b/bit_manipulation/power_of_4.py @@ -10,7 +10,8 @@ n - 1 = 0..011..11 n & (n - 1) - no intersections = 0 -If the number is a power of 4 then it should be a power of 2 and the set bit should be at an odd position +If the number is a power of 4 then it should be a power of 2 +and the set bit should be at an odd position """ @@ -51,7 +52,7 @@ def power_of_4(number: int) -> bool: while number: c += 1 number >>= 1 - return c % 2 + return c%2==1 else: return False From 6c87d478bad9bf46becf3e215cd0b8670cf8d5c1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 16:51:13 +0000 Subject: [PATCH 09/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/power_of_4.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/power_of_4.py b/bit_manipulation/power_of_4.py index 1777f8fe3f15..1e08cf7182a6 100644 --- a/bit_manipulation/power_of_4.py +++ b/bit_manipulation/power_of_4.py @@ -10,7 +10,7 @@ n - 1 = 0..011..11 n & (n - 1) - no intersections = 0 -If the number is a power of 4 then it should be a power of 2 +If the number is a power of 4 then it should be a power of 2 and the set bit should be at an odd position """ @@ -52,7 +52,7 @@ def power_of_4(number: int) -> bool: while number: c += 1 number >>= 1 - return c%2==1 + return c % 2 == 1 else: return False From 8743e192b04f611acc4a04b105404e957783302f Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 22:23:11 +0530 Subject: [PATCH 10/12] updated power_of_4 --- bit_manipulation/power_of_4.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/power_of_4.py b/bit_manipulation/power_of_4.py index 1777f8fe3f15..4058bcfcd550 100644 --- a/bit_manipulation/power_of_4.py +++ b/bit_manipulation/power_of_4.py @@ -10,8 +10,8 @@ n - 1 = 0..011..11 n & (n - 1) - no intersections = 0 -If the number is a power of 4 then it should be a power of 2 -and the set bit should be at an odd position +If the number is a power of 4 then it should be a power of 2 +and the set bit should be at an odd position. """ From fa18bdbcd0b9560657efc1d49e361a5a0d9080aa Mon Sep 17 00:00:00 2001 From: siddwarr Date: Thu, 5 Oct 2023 21:25:28 +0530 Subject: [PATCH 11/12] added type check --- bit_manipulation/power_of_4.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/power_of_4.py b/bit_manipulation/power_of_4.py index 5a48b9732879..570f6c4ed4a8 100644 --- a/bit_manipulation/power_of_4.py +++ b/bit_manipulation/power_of_4.py @@ -42,9 +42,11 @@ def power_of_4(number: int) -> bool: >>> power_of_4(1.2) Traceback (most recent call last): ... - TypeError: unsupported operand type(s) for &: 'float' and 'float' + TypeError: number must be an integer """ + if not isinstance(number, int): + raise TypeError("number must be an integer") if number <= 0: raise ValueError("number must be positive") if number & (number - 1) == 0: From 550fb3fde07e5b74161d07e86fbd783087fad710 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Fri, 6 Oct 2023 16:08:10 +0530 Subject: [PATCH 12/12] added tescase --- bit_manipulation/power_of_4.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bit_manipulation/power_of_4.py b/bit_manipulation/power_of_4.py index 570f6c4ed4a8..09e6e28621df 100644 --- a/bit_manipulation/power_of_4.py +++ b/bit_manipulation/power_of_4.py @@ -35,6 +35,8 @@ def power_of_4(number: int) -> bool: False >>> power_of_4(17) False + >>> power_of_4(64) + True >>> power_of_4(-1) Traceback (most recent call last): ...