From 953462c94228218b1b8d211c3b30c66ae0da8675 Mon Sep 17 00:00:00 2001 From: Anup Singh Date: Thu, 23 Apr 2026 10:28:53 +0530 Subject: [PATCH 1/3] fix: off-by-one error in binary_count_trailing_zeros for zero input #14479 --- .../binary_count_trailing_zeros.py | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index f401c4ab9266..5b3746a0b433 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -3,8 +3,10 @@ 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. + Return the number of trailing zeros in the binary representation of a non-negative integer. + + Note: + For a == 0, returns 0 by convention. >>> binary_count_trailing_zeros(25) 0 @@ -21,24 +23,27 @@ def binary_count_trailing_zeros(a: int) -> int: >>> binary_count_trailing_zeros(-10) Traceback (most recent call last): ... - ValueError: Input value must be a positive integer + ValueError: Input must be a non-negative integer >>> binary_count_trailing_zeros(0.8) Traceback (most recent call last): ... - TypeError: Input value must be a 'int' type + TypeError: Input must be an integer >>> binary_count_trailing_zeros("0") Traceback (most recent call last): ... - TypeError: '<' not supported between instances of 'str' and 'int' + TypeError: Input must be an integer """ - 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 0 if (a == 0) else int(log2(a & -a)) + + if not isinstance(a, int): + raise TypeError("Input must be an integer") + + + if a < 0: + raise ValueError("Input must be a non-negative integer") -if __name__ == "__main__": - import doctest + + if a == 0: + return 0 - doctest.testmod() + return int(log2(a & -a)) \ No newline at end of file From a432438b8b2e58d2250acf7eb08e4c927358eb23 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 23 Apr 2026 05:06:25 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/binary_count_trailing_zeros.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index 5b3746a0b433..3c15520c18cb 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -34,16 +34,13 @@ def binary_count_trailing_zeros(a: int) -> int: TypeError: Input must be an integer """ - if not isinstance(a, int): raise TypeError("Input must be an integer") - if a < 0: raise ValueError("Input must be a non-negative integer") - if a == 0: return 0 - return int(log2(a & -a)) \ No newline at end of file + return int(log2(a & -a)) From 6a111329336b5c0b31c06029585325a6d6b98cf0 Mon Sep 17 00:00:00 2001 From: Anup Singh Date: Thu, 23 Apr 2026 10:45:35 +0530 Subject: [PATCH 3/3] fix: apply pre-commit formatting and line length fix --- bit_manipulation/binary_count_trailing_zeros.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index 3c15520c18cb..5a6e0cdda6c7 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -3,7 +3,8 @@ def binary_count_trailing_zeros(a: int) -> int: """ - Return the number of trailing zeros in the binary representation of a non-negative integer. + Return the number of trailing zeros in the binary + representation of a non-negative integer. Note: For a == 0, returns 0 by convention.