From da2472a65233e56d12b6a89a11ca1e393bea5eda Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Tue, 1 Nov 2022 17:51:50 +0400 Subject: [PATCH 1/5] add is power of two --- bit_manipulation/is_power_of_two.py | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 bit_manipulation/is_power_of_two.py diff --git a/bit_manipulation/is_power_of_two.py b/bit_manipulation/is_power_of_two.py new file mode 100644 index 000000000000..9d5147ada828 --- /dev/null +++ b/bit_manipulation/is_power_of_two.py @@ -0,0 +1,40 @@ +""" +Author : Alexander Pantyukhin +Date : November 1, 2022 + +Task: +Given a positive number. Return True if this number is power of 2 +and False otherwise. + +Implementation notes: Use bit manipulation. +For example if the number is the power of two it's bits representation: +n = 0..100..00 +n - 1:0..011..11 + +n & (n - 1) - no intersections = 0 + +""" + +def is_power_of_two(number: int) -> bool: + """ + >>> is_power_of_two(2) + True + >>> is_power_of_two(4) + True + >>> is_power_of_two(6) + False + >>> is_power_of_two(8) + True + >>> is_power_of_two(17) + False + >>> is_power_of_two(32) + True + >>> is_power_of_two(31) + False + """ + + return number & (number - 1) == 0 + +if __name__ == "__main__": + import doctest + doctest.testmod() From d73b479100895a659259f9598c28b0be6abbddde Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Tue, 1 Nov 2022 17:53:53 +0400 Subject: [PATCH 2/5] fix comment --- bit_manipulation/is_power_of_two.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/is_power_of_two.py b/bit_manipulation/is_power_of_two.py index 9d5147ada828..c7716200c3aa 100644 --- a/bit_manipulation/is_power_of_two.py +++ b/bit_manipulation/is_power_of_two.py @@ -3,13 +3,13 @@ Date : November 1, 2022 Task: -Given a positive number. Return True if this number is power of 2 +Given a positive int number. Return True if this number is power of 2 and False otherwise. Implementation notes: Use bit manipulation. For example if the number is the power of two it's bits representation: -n = 0..100..00 -n - 1:0..011..11 +n = 0..100..00 +n - 1 = 0..011..11 n & (n - 1) - no intersections = 0 From da8111b3174baaf24a69092a11fcc46e66dc18c8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Nov 2022 13:57:05 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/is_power_of_two.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/is_power_of_two.py b/bit_manipulation/is_power_of_two.py index c7716200c3aa..622d28ee44ca 100644 --- a/bit_manipulation/is_power_of_two.py +++ b/bit_manipulation/is_power_of_two.py @@ -6,7 +6,7 @@ Given a positive int number. Return True if this number is power of 2 and False otherwise. -Implementation notes: Use bit manipulation. +Implementation notes: Use bit manipulation. For example if the number is the power of two it's bits representation: n = 0..100..00 n - 1 = 0..011..11 @@ -15,6 +15,7 @@ """ + def is_power_of_two(number: int) -> bool: """ >>> is_power_of_two(2) @@ -35,6 +36,8 @@ def is_power_of_two(number: int) -> bool: return number & (number - 1) == 0 + if __name__ == "__main__": import doctest + doctest.testmod() From 1a52a9c2de75166e50791c9fbf4b225140943cb3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 1 Nov 2022 18:34:36 +0100 Subject: [PATCH 4/5] Deal with negative numbers --- bit_manipulation/is_power_of_two.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/bit_manipulation/is_power_of_two.py b/bit_manipulation/is_power_of_two.py index 622d28ee44ca..8b79b1519720 100644 --- a/bit_manipulation/is_power_of_two.py +++ b/bit_manipulation/is_power_of_two.py @@ -4,7 +4,7 @@ Task: Given a positive int number. Return True if this number is power of 2 -and False otherwise. +or False otherwise. Implementation notes: Use bit manipulation. For example if the number is the power of two it's bits representation: @@ -12,12 +12,17 @@ n - 1 = 0..011..11 n & (n - 1) - no intersections = 0 - """ def is_power_of_two(number: int) -> bool: """ + Return True if this number is power of 2 or False otherwise. + + >>> is_power_of_two(0) + True + >>> is_power_of_two(1) + True >>> is_power_of_two(2) True >>> is_power_of_two(4) @@ -28,12 +33,21 @@ def is_power_of_two(number: int) -> bool: True >>> is_power_of_two(17) False - >>> is_power_of_two(32) + >>> is_power_of_two(-1) + Traceback (most recent call last): + ... + ValueError: number must not be negitive + >>> is_power_of_two(1.2) + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for &: 'float' and 'float' + + # Test all powers of 2 from 0 to 10,000 + >>> all(is_power_of_two(int(2 ** i)) for i in range(10000)) True - >>> is_power_of_two(31) - False """ - + if number < 0: + raise ValueError("number must not be negitive") return number & (number - 1) == 0 From 240c39104b6f84070916f51128c2b1be2f98294c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 1 Nov 2022 18:36:46 +0100 Subject: [PATCH 5/5] Spelling: negative --- bit_manipulation/is_power_of_two.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/is_power_of_two.py b/bit_manipulation/is_power_of_two.py index 8b79b1519720..023e979fe51c 100644 --- a/bit_manipulation/is_power_of_two.py +++ b/bit_manipulation/is_power_of_two.py @@ -36,7 +36,7 @@ def is_power_of_two(number: int) -> bool: >>> is_power_of_two(-1) Traceback (most recent call last): ... - ValueError: number must not be negitive + ValueError: number must not be negative >>> is_power_of_two(1.2) Traceback (most recent call last): ... @@ -47,7 +47,7 @@ def is_power_of_two(number: int) -> bool: True """ if number < 0: - raise ValueError("number must not be negitive") + raise ValueError("number must not be negative") return number & (number - 1) == 0