TheAlgorithms-Python
44 строки · 1.2 Кб
1from math import log22
3
4def binary_count_trailing_zeros(a: int) -> int:5"""6Take in 1 integer, return a number that is
7the number of trailing zeros in binary representation of that number.
8
9>>> binary_count_trailing_zeros(25)
100
11>>> binary_count_trailing_zeros(36)
122
13>>> binary_count_trailing_zeros(16)
144
15>>> binary_count_trailing_zeros(58)
161
17>>> binary_count_trailing_zeros(4294967296)
1832
19>>> binary_count_trailing_zeros(0)
200
21>>> binary_count_trailing_zeros(-10)
22Traceback (most recent call last):
23...
24ValueError: Input value must be a positive integer
25>>> binary_count_trailing_zeros(0.8)
26Traceback (most recent call last):
27...
28TypeError: Input value must be a 'int' type
29>>> binary_count_trailing_zeros("0")
30Traceback (most recent call last):
31...
32TypeError: '<' not supported between instances of 'str' and 'int'
33"""
34if a < 0:35raise ValueError("Input value must be a positive integer")36elif isinstance(a, float):37raise TypeError("Input value must be a 'int' type")38return 0 if (a == 0) else int(log2(a & -a))39
40
41if __name__ == "__main__":42import doctest43
44doctest.testmod()45