Fix argument validation for count_1s_brian_kernighan_method (#7994)

* Fix argument validation for count_1s_brian_kernighan_method

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Alexander Pantyukhin 2022-11-20 14:59:25 +04:00 committed by GitHub
parent f01a1af1df
commit a25c53e8b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View File

@ -571,6 +571,7 @@
* [Largest Subarray Sum](maths/largest_subarray_sum.py)
* [Least Common Multiple](maths/least_common_multiple.py)
* [Line Length](maths/line_length.py)
* [Liouville Lambda](maths/liouville_lambda.py)
* [Lucas Lehmer Primality Test](maths/lucas_lehmer_primality_test.py)
* [Lucas Series](maths/lucas_series.py)
* [Maclaurin Series](maths/maclaurin_series.py)

View File

@ -17,16 +17,19 @@ def get_1s_count(number: int) -> int:
>>> get_1s_count(-1)
Traceback (most recent call last):
...
ValueError: the value of input must be positive
ValueError: Input must be a non-negative integer
>>> get_1s_count(0.8)
Traceback (most recent call last):
...
TypeError: Input value must be an 'int' type
ValueError: Input must be a non-negative integer
>>> get_1s_count("25")
Traceback (most recent call last):
...
ValueError: Input must be a non-negative integer
"""
if number < 0:
raise ValueError("the value of input must be positive")
elif isinstance(number, float):
raise TypeError("Input value must be an 'int' type")
if not isinstance(number, int) or number < 0:
raise ValueError("Input must be a non-negative integer")
count = 0
while number:
# This way we arrive at next set bit (next 1) instead of looping