From 44fe6c20721c0717ecbc42ae6c6353e1e7e33eae Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Wed, 10 Feb 2021 10:02:55 +0530 Subject: [PATCH 1/2] count-bits --- bit_manipulation/count_number_of_one_bits.py | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 bit_manipulation/count_number_of_one_bits.py diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py new file mode 100644 index 000000000000..23aa9e87ee0d --- /dev/null +++ b/bit_manipulation/count_number_of_one_bits.py @@ -0,0 +1,48 @@ +def count_one_bits(number: int) -> int: + """ + Take in an 32 bit integer, count the number of one bits, + return the number of one bits + result of a count_one_bits and operation on the integer provided. + >>> count_one_bits(25) + 3 + >>> count_one_bits(37) + 3 + >>> count_one_bits(21) + 3 + >>> count_one_bits(58) + 4 + >>> count_one_bits(0) + 0 + >>> count_one_bits(256) + 1 + >>> count_one_bits(-1) + Traceback (most recent call last): + ... + ValueError: the value of input must be positive + >>> count_one_bits(1.1) + Traceback (most recent call last): + ... + TypeError: Input value must be a 'int' type + >>> count_one_bits("0") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if number < 0: + raise ValueError("the value of input must be positive") + elif isinstance(number, float): + raise TypeError("Input value must be a 'int' type") + elif isinstance(number, str): + raise TypeError("'<' not supported between instances of 'str' and 'int'") + result = 0 + while number: + if number % 2 == 1: + result += 1 + number = number >> 1 + return result + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 9e54101446f8984c2be9d59cf7610bab31d8c059 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Thu, 11 Feb 2021 20:45:13 +0530 Subject: [PATCH 2/2] update --- bit_manipulation/count_number_of_one_bits.py | 32 ++++++-------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/bit_manipulation/count_number_of_one_bits.py b/bit_manipulation/count_number_of_one_bits.py index 23aa9e87ee0d..51fd2b630483 100644 --- a/bit_manipulation/count_number_of_one_bits.py +++ b/bit_manipulation/count_number_of_one_bits.py @@ -1,39 +1,25 @@ -def count_one_bits(number: int) -> int: +def get_set_bits_count(number: int) -> int: """ - Take in an 32 bit integer, count the number of one bits, - return the number of one bits - result of a count_one_bits and operation on the integer provided. - >>> count_one_bits(25) + Count the number of set bits in a 32 bit integer + >>> get_set_bits_count(25) 3 - >>> count_one_bits(37) + >>> get_set_bits_count(37) 3 - >>> count_one_bits(21) + >>> get_set_bits_count(21) 3 - >>> count_one_bits(58) + >>> get_set_bits_count(58) 4 - >>> count_one_bits(0) + >>> get_set_bits_count(0) 0 - >>> count_one_bits(256) + >>> get_set_bits_count(256) 1 - >>> count_one_bits(-1) + >>> get_set_bits_count(-1) Traceback (most recent call last): ... ValueError: the value of input must be positive - >>> count_one_bits(1.1) - Traceback (most recent call last): - ... - TypeError: Input value must be a 'int' type - >>> count_one_bits("0") - Traceback (most recent call last): - ... - TypeError: '<' not supported between instances of 'str' and 'int' """ if number < 0: raise ValueError("the value of input must be positive") - elif isinstance(number, float): - raise TypeError("Input value must be a 'int' type") - elif isinstance(number, str): - raise TypeError("'<' not supported between instances of 'str' and 'int'") result = 0 while number: if number % 2 == 1: