From 80bce46523129f7409c1b3f1fc34c25ed4532ca0 Mon Sep 17 00:00:00 2001 From: Satwik Mohanty Date: Thu, 2 Apr 2026 02:23:42 +0530 Subject: [PATCH 1/2] Fix zero input bug in binary_count_trailing_zeros --- .../binary_count_trailing_zeros.py | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index f401c4ab9266..39b1e714f47e 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -3,8 +3,7 @@ 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. + Take in 1 integer, return the number of trailing zeros in binary representation. >>> binary_count_trailing_zeros(25) 0 @@ -17,7 +16,9 @@ def binary_count_trailing_zeros(a: int) -> int: >>> binary_count_trailing_zeros(4294967296) 32 >>> binary_count_trailing_zeros(0) - 0 + Traceback (most recent call last): + ... + ValueError: Trailing zeros for 0 are undefined >>> binary_count_trailing_zeros(-10) Traceback (most recent call last): ... @@ -25,20 +26,29 @@ def binary_count_trailing_zeros(a: int) -> int: >>> binary_count_trailing_zeros(0.8) Traceback (most recent call last): ... - TypeError: Input value must be a 'int' type + TypeError: Input value 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 value must be an integer """ + + # Type check + if not isinstance(a, int): + raise TypeError("Input value must be an integer") + + # Edge case: zero + if a == 0: + raise ValueError("Trailing zeros for 0 are undefined") + + # Negative numbers not allowed 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)) + + # Core logic + return int(log2(a & -a)) if __name__ == "__main__": import doctest - - doctest.testmod() + doctest.testmod() \ No newline at end of file From 071527e64d329dd3ddef68da10bcff4ac2a95cf2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 Apr 2026 20:55:32 +0000 Subject: [PATCH 2/2] [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 | 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 39b1e714f47e..e989d5086c8d 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -51,4 +51,5 @@ def binary_count_trailing_zeros(a: int) -> int: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod()