From 98c7772904b4d1c47b7e1066ca71dad36be458c3 Mon Sep 17 00:00:00 2001 From: GautamChaurasia Date: Sat, 15 Oct 2022 19:45:19 +0530 Subject: [PATCH 1/5] Added algorithm for finding index of rightmost set bit --- .../index_of_rightmost_set_bit.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 bit_manipulation/index_of_rightmost_set_bit.py diff --git a/bit_manipulation/index_of_rightmost_set_bit.py b/bit_manipulation/index_of_rightmost_set_bit.py new file mode 100644 index 000000000000..6607be00c696 --- /dev/null +++ b/bit_manipulation/index_of_rightmost_set_bit.py @@ -0,0 +1,41 @@ +"Reference: https://www.geeksforgeeks.org/position-of-rightmost-set-bit/" + +def get_index_of_rightmost_set_bit(number: int) -> int: + + """ + Take in a positive integer 'number'. + Returns the zero-based index of first set bit in that 'number' from right. + Returns -1, If no set bit found. + + >>> get_index_of_rightmost_set_bit(0) + -1 + >>> get_index_of_rightmost_set_bit(5) + 0 + >>> get_index_of_rightmost_set_bit(36) + 2 + >>> get_index_of_rightmost_set_bit(8) + 3 + >>> get_index_of_rightmost_set_bit(-18) + Traceback (most recent call last): + ... + ValueError: Input must be a non-negative integer + """ + + if number>=0 and type(number)==int: + intermediate = number&~(number-1) + index = 0 + while intermediate: + intermediate>>=1 + index+=1 + return index-1 + else: raise ValueError("Input must be a non-negative integer") + +""" +Finding the index of rightmost set bit has some very peculiar use-cases, +especially in finding missing or/and repeating numbers in a list of positive integers. +""" + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 071639b038d318fb53c4029732571f27b9a98b21 Mon Sep 17 00:00:00 2001 From: GautamChaurasia Date: Sat, 15 Oct 2022 20:06:57 +0530 Subject: [PATCH 2/5] applied suggested changes --- .../index_of_rightmost_set_bit.py | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/bit_manipulation/index_of_rightmost_set_bit.py b/bit_manipulation/index_of_rightmost_set_bit.py index 6607be00c696..fef6395b9f01 100644 --- a/bit_manipulation/index_of_rightmost_set_bit.py +++ b/bit_manipulation/index_of_rightmost_set_bit.py @@ -1,7 +1,6 @@ -"Reference: https://www.geeksforgeeks.org/position-of-rightmost-set-bit/" +# Reference: https://www.geeksforgeeks.org/position-of-rightmost-set-bit/ def get_index_of_rightmost_set_bit(number: int) -> int: - """ Take in a positive integer 'number'. Returns the zero-based index of first set bit in that 'number' from right. @@ -21,21 +20,21 @@ def get_index_of_rightmost_set_bit(number: int) -> int: ValueError: Input must be a non-negative integer """ - if number>=0 and type(number)==int: - intermediate = number&~(number-1) - index = 0 - while intermediate: - intermediate>>=1 - index+=1 - return index-1 - else: raise ValueError("Input must be a non-negative integer") + if 0 > number and not isinstance(number, int): + raise ValueError("Input must be a non-negative integer") -""" -Finding the index of rightmost set bit has some very peculiar use-cases, -especially in finding missing or/and repeating numbers in a list of positive integers. -""" + intermediate = number&~(number-1) + index = 0 + while intermediate: + intermediate>>=1 + index+=1 + return index-1 if __name__ == "__main__": + """ + Finding the index of rightmost set bit has some very peculiar use-cases, + especially in finding missing or/and repeating numbers in a list of positive integers. + """ import doctest doctest.testmod() From dde1fa39fc4d5c2bd0e1405bf5b1e5eed52f4f74 Mon Sep 17 00:00:00 2001 From: GautamChaurasia Date: Sat, 15 Oct 2022 20:11:32 +0530 Subject: [PATCH 3/5] applied suggested changes --- bit_manipulation/index_of_rightmost_set_bit.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/index_of_rightmost_set_bit.py b/bit_manipulation/index_of_rightmost_set_bit.py index fef6395b9f01..2fa0a771a72c 100644 --- a/bit_manipulation/index_of_rightmost_set_bit.py +++ b/bit_manipulation/index_of_rightmost_set_bit.py @@ -33,7 +33,8 @@ def get_index_of_rightmost_set_bit(number: int) -> int: if __name__ == "__main__": """ Finding the index of rightmost set bit has some very peculiar use-cases, - especially in finding missing or/and repeating numbers in a list of positive integers. + especially in finding missing or/and repeating numbers in a list of + positive integers. """ import doctest From b52bde6ffa9f994740929e17a37117af4fd531bb Mon Sep 17 00:00:00 2001 From: GautamChaurasia Date: Sat, 15 Oct 2022 20:27:10 +0530 Subject: [PATCH 4/5] Fixed failing Testcases --- bit_manipulation/index_of_rightmost_set_bit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/index_of_rightmost_set_bit.py b/bit_manipulation/index_of_rightmost_set_bit.py index 2fa0a771a72c..fbed4926f6fd 100644 --- a/bit_manipulation/index_of_rightmost_set_bit.py +++ b/bit_manipulation/index_of_rightmost_set_bit.py @@ -20,7 +20,7 @@ def get_index_of_rightmost_set_bit(number: int) -> int: ValueError: Input must be a non-negative integer """ - if 0 > number and not isinstance(number, int): + if number<0 or not isinstance(number, int): raise ValueError("Input must be a non-negative integer") intermediate = number&~(number-1) @@ -38,4 +38,4 @@ def get_index_of_rightmost_set_bit(number: int) -> int: """ import doctest - doctest.testmod() + doctest.testmod(verbose=True) From 2f6e5124595dee605b32f2d874388d0370618418 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 15 Oct 2022 14:58:31 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../index_of_rightmost_set_bit.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/bit_manipulation/index_of_rightmost_set_bit.py b/bit_manipulation/index_of_rightmost_set_bit.py index fbed4926f6fd..eb52ea4e63e3 100644 --- a/bit_manipulation/index_of_rightmost_set_bit.py +++ b/bit_manipulation/index_of_rightmost_set_bit.py @@ -1,34 +1,36 @@ # Reference: https://www.geeksforgeeks.org/position-of-rightmost-set-bit/ + def get_index_of_rightmost_set_bit(number: int) -> int: """ Take in a positive integer 'number'. Returns the zero-based index of first set bit in that 'number' from right. Returns -1, If no set bit found. - >>> get_index_of_rightmost_set_bit(0) + >>> get_index_of_rightmost_set_bit(0) -1 - >>> get_index_of_rightmost_set_bit(5) + >>> get_index_of_rightmost_set_bit(5) 0 - >>> get_index_of_rightmost_set_bit(36) + >>> get_index_of_rightmost_set_bit(36) 2 - >>> get_index_of_rightmost_set_bit(8) + >>> get_index_of_rightmost_set_bit(8) 3 - >>> get_index_of_rightmost_set_bit(-18) + >>> get_index_of_rightmost_set_bit(-18) Traceback (most recent call last): ... ValueError: Input must be a non-negative integer """ - if number<0 or not isinstance(number, int): + if number < 0 or not isinstance(number, int): raise ValueError("Input must be a non-negative integer") - intermediate = number&~(number-1) + intermediate = number & ~(number - 1) index = 0 while intermediate: - intermediate>>=1 - index+=1 - return index-1 + intermediate >>= 1 + index += 1 + return index - 1 + if __name__ == "__main__": """