Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions bit_manipulation/binary_count_trailing_zeros.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

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
Expand All @@ -21,24 +24,24 @@ 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))