From 350a220410aab857447cfb4048523ab538008ae1 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 09:49:53 +0530 Subject: [PATCH 01/13] added binary_count_trailing_zeros.py --- .../binary_count_trailing_zeros.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 bit_manipulation/binary_count_trailing_zeros.py diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py new file mode 100644 index 000000000000..9e7aba5931c2 --- /dev/null +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -0,0 +1,43 @@ +from math import log2 + + +def binary_count_trailing_zeros(a: int): + """ + Take in 1 integer, return a number that is the number of trailing zeros in binary representation of that number. + + >>> binary_count_trailing_zeros(25) + '0' + >>> binary_count_trailing_zeros(36) + '2' + >>> binary_count_trailing_zeros(16) + '4' + >>> binary_count_trailing_zeros(58) + '1' + >>> binary_count_trailing_zeros(4294967296) + '32' + >>> binary_count_trailing_zeros(0) + '0' + >>> binary_count_trailing_zeros(-10) + Traceback (most recent call last): + ... + ValueError: the value of input must be positive + >>> binary_count_trailing_zeros(0.8) + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> binary_count_trailing_zeros("0") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if a < 0: + raise ValueError("the value of input must be positive") + + # counting trailing zeros in O(1) time complexity + return (0 if (a == 0) else int(log2(a & -a))) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 4828ab8038ab51eacd815058af6c9053d3cae153 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 10:06:59 +0530 Subject: [PATCH 02/13] updated binary_count_trailing_zeros.py file --- bit_manipulation/binary_count_trailing_zeros.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index 9e7aba5931c2..da987175d9fa 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -6,17 +6,17 @@ def binary_count_trailing_zeros(a: int): Take in 1 integer, return a number that is the number of trailing zeros in binary representation of that number. >>> binary_count_trailing_zeros(25) - '0' + 0 >>> binary_count_trailing_zeros(36) - '2' + 2 >>> binary_count_trailing_zeros(16) - '4' + 4 >>> binary_count_trailing_zeros(58) - '1' + 1 >>> binary_count_trailing_zeros(4294967296) - '32' + 32 >>> binary_count_trailing_zeros(0) - '0' + 0 >>> binary_count_trailing_zeros(-10) Traceback (most recent call last): ... @@ -33,8 +33,7 @@ def binary_count_trailing_zeros(a: int): if a < 0: raise ValueError("the value of input must be positive") - # counting trailing zeros in O(1) time complexity - return (0 if (a == 0) else int(log2(a & -a))) + return 0 if (a == 0) else int(log2(a & -a)) if __name__ == "__main__": From 6300dd120af3eca9e247405ac7d631db0969e1e9 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 10:11:51 +0530 Subject: [PATCH 03/13] changed file name to count_trailing_zeros.py --- .../{binary_count_trailing_zeros.py => count_trailing_zeros.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bit_manipulation/{binary_count_trailing_zeros.py => count_trailing_zeros.py} (100%) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/count_trailing_zeros.py similarity index 100% rename from bit_manipulation/binary_count_trailing_zeros.py rename to bit_manipulation/count_trailing_zeros.py From 469327e5ba8300ffdd5b2a30da908e7e2dfdac44 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 10:14:41 +0530 Subject: [PATCH 04/13] updated count_trailing_zeros.py --- bit_manipulation/count_trailing_zeros.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/count_trailing_zeros.py b/bit_manipulation/count_trailing_zeros.py index da987175d9fa..eaa9f2b87819 100644 --- a/bit_manipulation/count_trailing_zeros.py +++ b/bit_manipulation/count_trailing_zeros.py @@ -24,7 +24,7 @@ def binary_count_trailing_zeros(a: int): >>> binary_count_trailing_zeros(0.8) Traceback (most recent call last): ... - TypeError: 'float' object cannot be interpreted as an integer + TypeError: unsupported operand type(s) for &: 'float' and 'float' >>> binary_count_trailing_zeros("0") Traceback (most recent call last): ... From fb565aa97dfe300558dfd2e7a1d289810f599bb1 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 10:22:40 +0530 Subject: [PATCH 05/13] resolved flake8 error --- bit_manipulation/count_trailing_zeros.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/count_trailing_zeros.py b/bit_manipulation/count_trailing_zeros.py index eaa9f2b87819..cbc7d9ee99c8 100644 --- a/bit_manipulation/count_trailing_zeros.py +++ b/bit_manipulation/count_trailing_zeros.py @@ -3,7 +3,8 @@ def binary_count_trailing_zeros(a: int): """ - Take in 1 integer, return a number that is the number of trailing zeros in binary representation of that number. + Take in 1 integer, return a number that is + the number of trailing zeros in binary representation of that number. >>> binary_count_trailing_zeros(25) 0 From 43b2577fee312063b1875812bb82197f5b038ace Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 10:57:43 +0530 Subject: [PATCH 06/13] renamed to binary_count_trailing_zeros.py --- .../{count_trailing_zeros.py => binary_count_trailing_zeros.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bit_manipulation/{count_trailing_zeros.py => binary_count_trailing_zeros.py} (100%) diff --git a/bit_manipulation/count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py similarity index 100% rename from bit_manipulation/count_trailing_zeros.py rename to bit_manipulation/binary_count_trailing_zeros.py From 581e4fd48a69fcc74ec900655007f79130faff0e Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 11:39:16 +0530 Subject: [PATCH 07/13] added required changes --- bit_manipulation/binary_count_trailing_zeros.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index cbc7d9ee99c8..166786cda6b0 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -1,7 +1,7 @@ from math import log2 -def binary_count_trailing_zeros(a: int): +def binary_count_trailing_zeros(a: int) -> int: """ Take in 1 integer, return a number that is the number of trailing zeros in binary representation of that number. @@ -21,19 +21,20 @@ def binary_count_trailing_zeros(a: int): >>> binary_count_trailing_zeros(-10) Traceback (most recent call last): ... - ValueError: the value of input must be positive + ValueError: Input value must be a positive integer >>> binary_count_trailing_zeros(0.8) Traceback (most recent call last): ... - TypeError: unsupported operand type(s) for &: 'float' and 'float' + TypeError: Input value must be a 'int' type >>> binary_count_trailing_zeros("0") Traceback (most recent call last): ... TypeError: '<' not supported between instances of 'str' and 'int' """ if a < 0: - raise ValueError("the value of input must be positive") - + raise ValueError("Input value must be a positive integer") + elif isinstance(a, float): + raise TypeError("Input value must be a \'int\' type") return 0 if (a == 0) else int(log2(a & -a)) From 44446053fd6aee19dc9fed904faa0cd92a4c6840 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 11:44:57 +0530 Subject: [PATCH 08/13] resolved pre-commit error --- bit_manipulation/binary_count_trailing_zeros.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index 166786cda6b0..f401c4ab9266 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -34,7 +34,7 @@ def binary_count_trailing_zeros(a: int) -> int: if a < 0: raise ValueError("Input value must be a positive integer") elif isinstance(a, float): - raise TypeError("Input value must be a \'int\' type") + raise TypeError("Input value must be a 'int' type") return 0 if (a == 0) else int(log2(a & -a)) From e7c4e371db1d13ce173fc43dd21fee76cc97dfc6 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 12:29:43 +0530 Subject: [PATCH 09/13] added count_setbits.py --- bit_manipulation/count_setbits.py | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 bit_manipulation/count_setbits.py diff --git a/bit_manipulation/count_setbits.py b/bit_manipulation/count_setbits.py new file mode 100644 index 000000000000..359fbc81d4fc --- /dev/null +++ b/bit_manipulation/count_setbits.py @@ -0,0 +1,44 @@ +from math import log2 + + +def count_setbits(a: int) -> int: + """ + Take in 1 integer, return a number that is + the number of 1's in binary representation of that number. + + >>> count_setbits(25) + 3 + >>> count_setbits(36) + 2 + >>> count_setbits(16) + 1 + >>> count_setbits(58) + 4 + >>> count_setbits(4294967295) + 32 + >>> count_setbits(0) + 0 + >>> count_setbits(-10) + Traceback (most recent call last): + ... + ValueError: Input value must be a positive integer + >>> count_setbits(0.8) + Traceback (most recent call last): + ... + TypeError: Input value must be a 'int' type + >>> count_setbits("0") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if a < 0: + raise ValueError("Input value must be a positive integer") + elif isinstance(a, float): + raise TypeError("Input value must be a 'int' type") + return bin(a).count("1") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From d644b717081634cb34820ec1af7fc2e077558e34 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 12:32:25 +0530 Subject: [PATCH 10/13] resolved errors --- bit_manipulation/count_setbits.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/bit_manipulation/count_setbits.py b/bit_manipulation/count_setbits.py index 359fbc81d4fc..e2b4e5d14fba 100644 --- a/bit_manipulation/count_setbits.py +++ b/bit_manipulation/count_setbits.py @@ -1,5 +1,3 @@ -from math import log2 - def count_setbits(a: int) -> int: """ From efdcd5badab95cab964bcf54a807d9d8d85ca401 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 12:33:44 +0530 Subject: [PATCH 11/13] changed name to binary_count_setbits.py --- bit_manipulation/{count_setbits.py => binary_count_setbits.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bit_manipulation/{count_setbits.py => binary_count_setbits.py} (100%) diff --git a/bit_manipulation/count_setbits.py b/bit_manipulation/binary_count_setbits.py similarity index 100% rename from bit_manipulation/count_setbits.py rename to bit_manipulation/binary_count_setbits.py From baeeaf06a888e4c958aaaf73bd5821c07c1390d9 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 12:35:57 +0530 Subject: [PATCH 12/13] updated file --- bit_manipulation/binary_count_setbits.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bit_manipulation/binary_count_setbits.py b/bit_manipulation/binary_count_setbits.py index e2b4e5d14fba..6a46495db185 100644 --- a/bit_manipulation/binary_count_setbits.py +++ b/bit_manipulation/binary_count_setbits.py @@ -1,30 +1,30 @@ -def count_setbits(a: int) -> int: +def binary_count_setbits(a: int) -> int: """ Take in 1 integer, return a number that is the number of 1's in binary representation of that number. - >>> count_setbits(25) + >>> binary_count_setbits(25) 3 - >>> count_setbits(36) + >>> binary_count_setbits(36) 2 - >>> count_setbits(16) + >>> binary_count_setbits(16) 1 - >>> count_setbits(58) + >>> binary_count_setbits(58) 4 - >>> count_setbits(4294967295) + >>> binary_count_setbits(4294967295) 32 - >>> count_setbits(0) + >>> binary_count_setbits(0) 0 - >>> count_setbits(-10) + >>> binary_count_setbits(-10) Traceback (most recent call last): ... ValueError: Input value must be a positive integer - >>> count_setbits(0.8) + >>> binary_count_setbits(0.8) Traceback (most recent call last): ... TypeError: Input value must be a 'int' type - >>> count_setbits("0") + >>> binary_count_setbits("0") Traceback (most recent call last): ... TypeError: '<' not supported between instances of 'str' and 'int' From 2844c258c38757506c1d17722ed5c99a99c8a322 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Thu, 1 Oct 2020 12:43:07 +0530 Subject: [PATCH 13/13] reformated file --- bit_manipulation/binary_count_setbits.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bit_manipulation/binary_count_setbits.py b/bit_manipulation/binary_count_setbits.py index 6a46495db185..3c92694533aa 100644 --- a/bit_manipulation/binary_count_setbits.py +++ b/bit_manipulation/binary_count_setbits.py @@ -1,4 +1,3 @@ - def binary_count_setbits(a: int) -> int: """ Take in 1 integer, return a number that is