mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 21:41:08 +00:00
ac6a160f1b
* added binary_count_trailing_zeros.py * updated binary_count_trailing_zeros.py file * changed file name to count_trailing_zeros.py * updated count_trailing_zeros.py * resolved flake8 error * renamed to binary_count_trailing_zeros.py * added required changes * resolved pre-commit error * added count_setbits.py * resolved errors * changed name to binary_count_setbits.py * updated file * reformated file
42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
def binary_count_setbits(a: int) -> int:
|
|
"""
|
|
Take in 1 integer, return a number that is
|
|
the number of 1's in binary representation of that number.
|
|
|
|
>>> binary_count_setbits(25)
|
|
3
|
|
>>> binary_count_setbits(36)
|
|
2
|
|
>>> binary_count_setbits(16)
|
|
1
|
|
>>> binary_count_setbits(58)
|
|
4
|
|
>>> binary_count_setbits(4294967295)
|
|
32
|
|
>>> binary_count_setbits(0)
|
|
0
|
|
>>> binary_count_setbits(-10)
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: Input value must be a positive integer
|
|
>>> binary_count_setbits(0.8)
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: Input value must be a 'int' type
|
|
>>> binary_count_setbits("0")
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: '<' not supported between instances of 'str' and 'int'
|
|
"""
|
|
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 bin(a).count("1")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|