From bb9c95a7a0c0038fde29579a90358fbc357b276d Mon Sep 17 00:00:00 2001 From: mehul-sweeti-agrawal Date: Mon, 2 Oct 2023 16:28:12 +0530 Subject: [PATCH 1/6] added is_power_of_four.py --- bit_manipulation/is_power_of_four.py | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 bit_manipulation/is_power_of_four.py diff --git a/bit_manipulation/is_power_of_four.py b/bit_manipulation/is_power_of_four.py new file mode 100644 index 000000000000..e4368083a90e --- /dev/null +++ b/bit_manipulation/is_power_of_four.py @@ -0,0 +1,32 @@ +""" +Author :- mehul-sweeti-agrawal + +Task :- Given an integer N, find whether that integer is a power of 4 or not. + + Input - N + Output - Yes/No + +""" + +def power_of_four(N): + + #For non-positive numbers + if N <= 0: + raise ValueError("number must be positive") + + #If number is a power of 2 and ends with 4 or 6 + if (N & (N-1) == 0) and (N%10 == 6 or N%10 == 4): + return True + else: + return False + + +if __name__ == "__main__": + + import doctest + doctest.testmod() + + + + + \ No newline at end of file From 19b74463b15ddd908f13959041c033c38feddbe9 Mon Sep 17 00:00:00 2001 From: mehul-sweeti-agrawal Date: Mon, 2 Oct 2023 16:40:40 +0530 Subject: [PATCH 2/6] added tests to is_power_of_four.py --- bit_manipulation/is_power_of_four.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/is_power_of_four.py b/bit_manipulation/is_power_of_four.py index e4368083a90e..32953596969b 100644 --- a/bit_manipulation/is_power_of_four.py +++ b/bit_manipulation/is_power_of_four.py @@ -9,7 +9,20 @@ """ def power_of_four(N): - + """ + Returns whether a number is a power of 4 or not + >>> power_of_four(8) + False + >>> power_of_four(4) + True + >>> power_of_four(16) + True + >>> power_of_four(-1) + Traceback (most recent call last): + ... + ValueError: number must be positive + """ + #For non-positive numbers if N <= 0: raise ValueError("number must be positive") From ccb2d2cce878c07c4724fc90357d8db2d4520d27 Mon Sep 17 00:00:00 2001 From: mehul-sweeti-agrawal Date: Mon, 2 Oct 2023 17:26:56 +0530 Subject: [PATCH 3/6] added input parameter type and return type to my function --- bit_manipulation/is_power_of_four.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/bit_manipulation/is_power_of_four.py b/bit_manipulation/is_power_of_four.py index 32953596969b..190000b4e86b 100644 --- a/bit_manipulation/is_power_of_four.py +++ b/bit_manipulation/is_power_of_four.py @@ -8,19 +8,23 @@ """ -def power_of_four(N): +def is_power_of_four(N: int) -> bool: """ Returns whether a number is a power of 4 or not - >>> power_of_four(8) + >>> is_power_of_four(8) False - >>> power_of_four(4) + >>> is_power_of_four(4) True - >>> power_of_four(16) + >>> is_power_of_four(16) True - >>> power_of_four(-1) + >>> is_power_of_four(-1) Traceback (most recent call last): ... ValueError: number must be positive + >>> is_power_of_four(9.8) + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for &: 'float' and 'float' """ #For non-positive numbers @@ -35,8 +39,8 @@ def power_of_four(N): if __name__ == "__main__": - import doctest + doctest.testmod() From c0b075444756e5fe6740ef060175aa4039ce9ef3 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 12:08:05 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/is_power_of_four.py | 32 ++++++++++++---------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/bit_manipulation/is_power_of_four.py b/bit_manipulation/is_power_of_four.py index 190000b4e86b..b46eb612fc84 100644 --- a/bit_manipulation/is_power_of_four.py +++ b/bit_manipulation/is_power_of_four.py @@ -8,8 +8,9 @@ """ + def is_power_of_four(N: int) -> bool: - """ + """ Returns whether a number is a power of 4 or not >>> is_power_of_four(8) False @@ -25,25 +26,20 @@ def is_power_of_four(N: int) -> bool: Traceback (most recent call last): ... TypeError: unsupported operand type(s) for &: 'float' and 'float' - """ - - #For non-positive numbers - if N <= 0: - raise ValueError("number must be positive") - - #If number is a power of 2 and ends with 4 or 6 - if (N & (N-1) == 0) and (N%10 == 6 or N%10 == 4): - return True - else: - return False - + """ -if __name__ == "__main__": - import doctest - - doctest.testmod() + # For non-positive numbers + if N <= 0: + raise ValueError("number must be positive") + # If number is a power of 2 and ends with 4 or 6 + if (N & (N - 1) == 0) and (N % 10 == 6 or N % 10 == 4): + return True + else: + return False +if __name__ == "__main__": + import doctest - \ No newline at end of file + doctest.testmod() From 184933a7f9b6235e401034fe3a31a0d50fe40feb Mon Sep 17 00:00:00 2001 From: mehul-sweeti-agrawal Date: Mon, 2 Oct 2023 18:21:43 +0530 Subject: [PATCH 5/6] added decimal_to_binary conversion --- bit_manipulation/decimal_to_binary.py | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 bit_manipulation/decimal_to_binary.py diff --git a/bit_manipulation/decimal_to_binary.py b/bit_manipulation/decimal_to_binary.py new file mode 100644 index 000000000000..5efb0a3bd333 --- /dev/null +++ b/bit_manipulation/decimal_to_binary.py @@ -0,0 +1,45 @@ +""" +Author :- mehul-sweeti-agrawal + +Task :- Given a positive decimal integer, convert it to binary + + Input - 9 + Output - 1001 + +""" + +def convert_to_binary(number: int) -> int: + + """ + Returns binary equivalent of a decimal number + >>> convert_to_binary(8) + 1000 + >>> convert_to_binary(5) + 101 + >>> convert_to_binary(-3) + Traceback (most recent call last): + ... + ValueError: number must be non-negative + >>> convert_to_binary(9.8) + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for &: 'float' and 'int' + """ + + #For negative numbers + if number < 0: + raise ValueError("number must be non-negative") + + power = 1 #helper variable + ans = 0 #stores binary equivalent of decimal number + while number: + if number & 1: + ans += power + power *= 10 + number >>= 1 + return ans + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 3f6ebde33aa6a2ad3510ddca5e6ca5bd9a8ee314 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 13:00:16 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/decimal_to_binary.py | 37 ++++++++++++++------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/bit_manipulation/decimal_to_binary.py b/bit_manipulation/decimal_to_binary.py index 5efb0a3bd333..46a6243224f6 100644 --- a/bit_manipulation/decimal_to_binary.py +++ b/bit_manipulation/decimal_to_binary.py @@ -8,30 +8,30 @@ """ + def convert_to_binary(number: int) -> int: - """ - Returns binary equivalent of a decimal number - >>> convert_to_binary(8) - 1000 - >>> convert_to_binary(5) - 101 - >>> convert_to_binary(-3) - Traceback (most recent call last): - ... - ValueError: number must be non-negative - >>> convert_to_binary(9.8) - Traceback (most recent call last): - ... - TypeError: unsupported operand type(s) for &: 'float' and 'int' + Returns binary equivalent of a decimal number + >>> convert_to_binary(8) + 1000 + >>> convert_to_binary(5) + 101 + >>> convert_to_binary(-3) + Traceback (most recent call last): + ... + ValueError: number must be non-negative + >>> convert_to_binary(9.8) + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for &: 'float' and 'int' """ - #For negative numbers + # For negative numbers if number < 0: - raise ValueError("number must be non-negative") + raise ValueError("number must be non-negative") - power = 1 #helper variable - ans = 0 #stores binary equivalent of decimal number + power = 1 # helper variable + ans = 0 # stores binary equivalent of decimal number while number: if number & 1: ans += power @@ -39,6 +39,7 @@ def convert_to_binary(number: int) -> int: number >>= 1 return ans + if __name__ == "__main__": import doctest