From 2d5e8f291154d2461c073896eee697f90c206e0f Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 19:16:15 +0530 Subject: [PATCH 01/16] added largest_power_of_2 --- bit_manipulation/largest_power_of_2 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 bit_manipulation/largest_power_of_2 diff --git a/bit_manipulation/largest_power_of_2 b/bit_manipulation/largest_power_of_2 new file mode 100644 index 000000000000..6e1265dc7560 --- /dev/null +++ b/bit_manipulation/largest_power_of_2 @@ -0,0 +1,18 @@ +def largest_power_of_2(number: int) -> int: + """ + returns the largest power of 2 that is less than or equal to the given number + The way this works is that we shift the binary form of the number to the right until we reach the last set bit + Using the number of times we had to shift to find the last set bit, we find the 2**(no of times shifted) which will be the ans + """ + + last_set_bit = 0 + while number: + last_set_bit+=1 + number>>=1 + return 2**(last_set_bit-1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 8be047901a4d258caf33cd83618c87b8ae5b33f3 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 19:21:45 +0530 Subject: [PATCH 02/16] updated_largest_power_of_2 --- bit_manipulation/{largest_power_of_2 => largest_power_of_2.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bit_manipulation/{largest_power_of_2 => largest_power_of_2.py} (100%) diff --git a/bit_manipulation/largest_power_of_2 b/bit_manipulation/largest_power_of_2.py similarity index 100% rename from bit_manipulation/largest_power_of_2 rename to bit_manipulation/largest_power_of_2.py From 71e4afb1fb2aa31c540e29afeb8cc287b5c798bc 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:53:37 +0000 Subject: [PATCH 03/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/largest_power_of_2.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index 6e1265dc7560..ca466390682b 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -7,9 +7,9 @@ def largest_power_of_2(number: int) -> int: last_set_bit = 0 while number: - last_set_bit+=1 - number>>=1 - return 2**(last_set_bit-1) + last_set_bit += 1 + number >>= 1 + return 2 ** (last_set_bit - 1) if __name__ == "__main__": From 81d29a04758716bef5e68a02a9e489d8c168770c Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 19:27:53 +0530 Subject: [PATCH 04/16] updated largest_power_of_2 --- bit_manipulation/largest_power_of_2.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index 6e1265dc7560..cb7ef966f3f4 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -3,6 +3,23 @@ def largest_power_of_2(number: int) -> int: returns the largest power of 2 that is less than or equal to the given number The way this works is that we shift the binary form of the number to the right until we reach the last set bit Using the number of times we had to shift to find the last set bit, we find the 2**(no of times shifted) which will be the ans + >>> largest_pow_of_two_less_than_or_equal_to_a_number(0) + 0 + >>> largest_pow_of_two_less_than_or_equal_to_a_number(1) + 1 + >>> largest_pow_of_two_less_than_or_equal_to_a_number(-1) + 0 + >>> largest_pow_of_two_less_than_or_equal_to_a_number(3) + 2 + >>> largest_pow_of_two_less_than_or_equal_to_a_number(15) + 8 + >>> largest_pow_of_two_less_than_or_equal_to_a_number(99) + 64 + >>> largest_pow_of_two_less_than_or_equal_to_a_number(178) + 128 + >>> largest_pow_of_two_less_than_or_equal_to_a_number(999999) + 524288 + """ last_set_bit = 0 From 67dc7d961913748b62d0a1d1714b751ae8eadccc 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:59:01 +0000 Subject: [PATCH 05/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/largest_power_of_2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index 4a99a9682880..54a1fc374c2b 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -19,7 +19,7 @@ def largest_power_of_2(number: int) -> int: 128 >>> largest_pow_of_two_less_than_or_equal_to_a_number(999999) 524288 - + """ last_set_bit = 0 From a8be9c2275e72903d573a35dadd2f0103666a7b6 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 19:59:38 +0530 Subject: [PATCH 06/16] updated largest_power_of_2 --- bit_manipulation/largest_power_of_2.py | 20 +++++++++++--------- bit_manipulation/tempCodeRunnerFile.py | 2 ++ 2 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 bit_manipulation/tempCodeRunnerFile.py diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index 4a99a9682880..32852458547d 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -3,21 +3,21 @@ def largest_power_of_2(number: int) -> int: returns the largest power of 2 that is less than or equal to the given number The way this works is that we shift the binary form of the number to the right until we reach the last set bit Using the number of times we had to shift to find the last set bit, we find the 2**(no of times shifted) which will be the ans - >>> largest_pow_of_two_less_than_or_equal_to_a_number(0) + >>> largest_power_of_2(0) 0 - >>> largest_pow_of_two_less_than_or_equal_to_a_number(1) + >>> largest_power_of_2(1) 1 - >>> largest_pow_of_two_less_than_or_equal_to_a_number(-1) + >>> largest_power_of_2(-1) 0 - >>> largest_pow_of_two_less_than_or_equal_to_a_number(3) + >>> largest_power_of_2(3) 2 - >>> largest_pow_of_two_less_than_or_equal_to_a_number(15) + >>> largest_power_of_2(15) 8 - >>> largest_pow_of_two_less_than_or_equal_to_a_number(99) + >>> largest_power_of_2(99) 64 - >>> largest_pow_of_two_less_than_or_equal_to_a_number(178) + >>> largest_power_of_2(178) 128 - >>> largest_pow_of_two_less_than_or_equal_to_a_number(999999) + >>> largest_power_of_2(999999) 524288 """ @@ -26,7 +26,9 @@ def largest_power_of_2(number: int) -> int: while number: last_set_bit += 1 number >>= 1 - return 2 ** (last_set_bit - 1) + if last_set_bit==0: + return 0 + return 2**(last_set_bit - 1) if __name__ == "__main__": diff --git a/bit_manipulation/tempCodeRunnerFile.py b/bit_manipulation/tempCodeRunnerFile.py new file mode 100644 index 000000000000..18520764b5b6 --- /dev/null +++ b/bit_manipulation/tempCodeRunnerFile.py @@ -0,0 +1,2 @@ +if last_set_bit==0: + # return 0 \ No newline at end of file From 834faa0feb2e133594ae2060243066541c86aa4e 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 14:30:21 +0000 Subject: [PATCH 07/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/largest_power_of_2.py | 4 ++-- bit_manipulation/tempCodeRunnerFile.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index 60bdc7b8fba6..12845915fdac 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -26,9 +26,9 @@ def largest_power_of_2(number: int) -> int: while number: last_set_bit += 1 number >>= 1 - if last_set_bit==0: + if last_set_bit == 0: return 0 - return 2**(last_set_bit - 1) + return 2 ** (last_set_bit - 1) if __name__ == "__main__": diff --git a/bit_manipulation/tempCodeRunnerFile.py b/bit_manipulation/tempCodeRunnerFile.py index 18520764b5b6..add77d773202 100644 --- a/bit_manipulation/tempCodeRunnerFile.py +++ b/bit_manipulation/tempCodeRunnerFile.py @@ -1,2 +1,2 @@ if last_set_bit==0: - # return 0 \ No newline at end of file + # return 0 From 24a040aa6b4ca540bc79cb638b987de55780caaa Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 20:04:21 +0530 Subject: [PATCH 08/16] updated largest_power_of_2 --- bit_manipulation/largest_power_of_2.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index 12845915fdac..ab0c3006052f 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -7,8 +7,6 @@ def largest_power_of_2(number: int) -> int: 0 >>> largest_power_of_2(1) 1 - >>> largest_power_of_2(-1) - 0 >>> largest_power_of_2(3) 2 >>> largest_power_of_2(15) From d0012f9172c6bd54455afcaf484c256b178e6c6b Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 13:07:59 +0530 Subject: [PATCH 09/16] updated largest_power_of_2 --- bit_manipulation/largest_power_of_2.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index ab0c3006052f..1c466a08f4ca 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -1,8 +1,11 @@ def largest_power_of_2(number: int) -> int: """ - returns the largest power of 2 that is less than or equal to the given number - The way this works is that we shift the binary form of the number to the right until we reach the last set bit - Using the number of times we had to shift to find the last set bit, we find the 2**(no of times shifted) which will be the ans + returns the largest power of 2 that is less than + or equal to the given number + The way this works is that we shift the binary form of the number + to the right until we reach the last set bit + Using the number of times we had to shift to find the last set bit, + we find the 2**(no of times shifted) which will be the ans >>> largest_power_of_2(0) 0 >>> largest_power_of_2(1) From 7aef7b75a0471aaab6c64f71289d7de6624c4f8b Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 13:10:22 +0530 Subject: [PATCH 10/16] updated largest_power_of_2 --- bit_manipulation/largest_power_of_2.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index 1c466a08f4ca..a79eaf283544 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -22,14 +22,14 @@ def largest_power_of_2(number: int) -> int: 524288 """ - - last_set_bit = 0 - while number: - last_set_bit += 1 - number >>= 1 - if last_set_bit == 0: + if number==0: return 0 - return 2 ** (last_set_bit - 1) + else: + last_set_bit = 0 + while number: + last_set_bit += 1 + number >>= 1 + return 2 ** (last_set_bit - 1) if __name__ == "__main__": From 2b2cb634903d44f2659de42f6f6ea72719936425 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:40:59 +0000 Subject: [PATCH 11/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/largest_power_of_2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index a79eaf283544..b3ebe9249d20 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -22,7 +22,7 @@ def largest_power_of_2(number: int) -> int: 524288 """ - if number==0: + if number == 0: return 0 else: last_set_bit = 0 From 2864ef8cc31918dd0569935f8a436362f905f53d Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 13:14:51 +0530 Subject: [PATCH 12/16] updated code --- bit_manipulation/largest_power_of_2.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index a79eaf283544..cd878fe6e331 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -29,8 +29,7 @@ def largest_power_of_2(number: int) -> int: while number: last_set_bit += 1 number >>= 1 - return 2 ** (last_set_bit - 1) - + return 2**(last_set_bit - 1) if __name__ == "__main__": import doctest From e6ea6a8eb6569c1816793e7dbff31a8255232981 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 07:45:33 +0000 Subject: [PATCH 13/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/largest_power_of_2.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index 70b02d90e520..b3ebe9249d20 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -29,7 +29,8 @@ def largest_power_of_2(number: int) -> int: while number: last_set_bit += 1 number >>= 1 - return 2**(last_set_bit - 1) + return 2 ** (last_set_bit - 1) + if __name__ == "__main__": import doctest From b1ac1f1495b768e30631ff7161186cd6b26d5bcb Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 17:35:05 +0530 Subject: [PATCH 14/16] updated code --- bit_manipulation/largest_power_of_2.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index b3ebe9249d20..14237faf23c2 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -4,7 +4,7 @@ def largest_power_of_2(number: int) -> int: or equal to the given number The way this works is that we shift the binary form of the number to the right until we reach the last set bit - Using the number of times we had to shift to find the last set bit, + Using the number of times we had to shift to find the last set bit we find the 2**(no of times shifted) which will be the ans >>> largest_power_of_2(0) 0 @@ -20,16 +20,16 @@ def largest_power_of_2(number: int) -> int: 128 >>> largest_power_of_2(999999) 524288 - """ + if number == 0: return 0 else: last_set_bit = 0 while number: - last_set_bit += 1 + last_set_bit+=1 number >>= 1 - return 2 ** (last_set_bit - 1) + return 2**(last_set_bit - 1) if __name__ == "__main__": From 6cbd4138d77e41558c7b367d48ef0717fdecf6ab Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:05:42 +0000 Subject: [PATCH 15/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/largest_power_of_2.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/largest_power_of_2.py b/bit_manipulation/largest_power_of_2.py index 14237faf23c2..4b285b449e7b 100644 --- a/bit_manipulation/largest_power_of_2.py +++ b/bit_manipulation/largest_power_of_2.py @@ -21,15 +21,15 @@ def largest_power_of_2(number: int) -> int: >>> largest_power_of_2(999999) 524288 """ - + if number == 0: return 0 else: last_set_bit = 0 while number: - last_set_bit+=1 + last_set_bit += 1 number >>= 1 - return 2**(last_set_bit - 1) + return 2 ** (last_set_bit - 1) if __name__ == "__main__": From 1c8f8438b1420db4139540369b7c6f9bd78ef76a Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 17:41:58 +0530 Subject: [PATCH 16/16] updated code --- bit_manipulation/tempCodeRunnerFile.py | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 bit_manipulation/tempCodeRunnerFile.py diff --git a/bit_manipulation/tempCodeRunnerFile.py b/bit_manipulation/tempCodeRunnerFile.py deleted file mode 100644 index add77d773202..000000000000 --- a/bit_manipulation/tempCodeRunnerFile.py +++ /dev/null @@ -1,2 +0,0 @@ -if last_set_bit==0: - # return 0