From 0037ffe0599b71783bc78a81e0ea316892ad90f2 Mon Sep 17 00:00:00 2001 From: Sharnabh Banerjee Date: Wed, 4 Oct 2023 20:10:54 +0530 Subject: [PATCH 1/4] Fixes #9374 Fixes #9374 --- ..._2_less_than_or_equal_to_a_given_number.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py diff --git a/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py b/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py new file mode 100644 index 000000000000..8d914f3bde0c --- /dev/null +++ b/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py @@ -0,0 +1,41 @@ +""" +Fixes #9374 +Name : Sharnabh Banerjee + +""" + + +def add_largest(num: int) -> int: + """ + Add Largest Power of 2 less then or equal to a given number + >>> add_largest(5.3) + Traceback (most recent call last): + ... + TypeError: num must be an integer !! + >>> add_largest(5) + 9 + >>> add_largest(10) + 18 + >>> add_largest(99) + 163 + >>> add_largest(-10) + 0 + >>> add_largest(999) + 1511 + + """ + if isinstance(num, float): + raise TypeError("num must be an integer !!") + if num <= 0: + return 0 + res = 1 + while (res << 1) <= num: + res <<= 1 + res += num + return res + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From f518c1ff60509087ab5cc9ee273b1f7ceb4713f3 Mon Sep 17 00:00:00 2001 From: Sharnabh Banerjee Date: Wed, 4 Oct 2023 20:15:49 +0530 Subject: [PATCH 2/4] Fixes #9374 Fixes #9374 --- ...largest_power_of_2_less_than_or_equal_to_a_given_number.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py b/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py index 8d914f3bde0c..82dd776ba445 100644 --- a/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py +++ b/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py @@ -24,11 +24,15 @@ def add_largest(num: int) -> int: 1511 """ + + # Checks if Float or not if isinstance(num, float): raise TypeError("num must be an integer !!") + # Checks if negative or Zero if num <= 0: return 0 res = 1 + # Left Bit Shift till it res is less than or equal to the given number while (res << 1) <= num: res <<= 1 res += num From 93d4444f4bf64c3d8d861fc2061ff0005c75bd52 Mon Sep 17 00:00:00 2001 From: Sharnabh Banerjee Date: Thu, 5 Oct 2023 12:47:24 +0530 Subject: [PATCH 3/4] Update bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py Co-authored-by: Chris O <46587501+ChrisO345@users.noreply.github.com> --- ...dd_largest_power_of_2_less_than_or_equal_to_a_given_number.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py b/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py index 82dd776ba445..77bc71ee549f 100644 --- a/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py +++ b/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py @@ -35,7 +35,6 @@ def add_largest(num: int) -> int: # Left Bit Shift till it res is less than or equal to the given number while (res << 1) <= num: res <<= 1 - res += num return res From 50ff9c09abe518733b69fba138a4c4d2f85ec344 Mon Sep 17 00:00:00 2001 From: Sharnabh Banerjee Date: Thu, 5 Oct 2023 13:00:35 +0530 Subject: [PATCH 4/4] Fixes #9347 Fixes #9347 The Function adds largest power of 2 less than or equal to a given number. --- ...argest_power_of_2_less_than_or_equal_to_a_given_number.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py b/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py index 82dd776ba445..9e278cf4dd50 100644 --- a/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py +++ b/bit_manipulation/add_largest_power_of_2_less_than_or_equal_to_a_given_number.py @@ -1,5 +1,5 @@ """ -Fixes #9374 +Fixes #9347 Name : Sharnabh Banerjee """ @@ -35,8 +35,7 @@ def add_largest(num: int) -> int: # Left Bit Shift till it res is less than or equal to the given number while (res << 1) <= num: res <<= 1 - res += num - return res + return res + num if __name__ == "__main__":