TheAlgorithms-Python

Форк
0
/
binary_count_trailing_zeros.py 
44 строки · 1.2 Кб
1
from math import log2
2

3

4
def binary_count_trailing_zeros(a: int) -> int:
5
    """
6
    Take in 1 integer, return a number that is
7
    the number of trailing zeros in binary representation of that number.
8

9
    >>> binary_count_trailing_zeros(25)
10
    0
11
    >>> binary_count_trailing_zeros(36)
12
    2
13
    >>> binary_count_trailing_zeros(16)
14
    4
15
    >>> binary_count_trailing_zeros(58)
16
    1
17
    >>> binary_count_trailing_zeros(4294967296)
18
    32
19
    >>> binary_count_trailing_zeros(0)
20
    0
21
    >>> binary_count_trailing_zeros(-10)
22
    Traceback (most recent call last):
23
        ...
24
    ValueError: Input value must be a positive integer
25
    >>> binary_count_trailing_zeros(0.8)
26
    Traceback (most recent call last):
27
        ...
28
    TypeError: Input value must be a 'int' type
29
    >>> binary_count_trailing_zeros("0")
30
    Traceback (most recent call last):
31
        ...
32
    TypeError: '<' not supported between instances of 'str' and 'int'
33
    """
34
    if a < 0:
35
        raise ValueError("Input value must be a positive integer")
36
    elif isinstance(a, float):
37
        raise TypeError("Input value must be a 'int' type")
38
    return 0 if (a == 0) else int(log2(a & -a))
39

40

41
if __name__ == "__main__":
42
    import doctest
43

44
    doctest.testmod()
45

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.