Skip to content

Commit a25c53e

Browse files
alexpantyukhingithub-actions
and
github-actions
authored
Fix argument validation for count_1s_brian_kernighan_method (TheAlgorithms#7994)
* Fix argument validation for count_1s_brian_kernighan_method * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent f01a1af commit a25c53e

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@
571571
* [Largest Subarray Sum](maths/largest_subarray_sum.py)
572572
* [Least Common Multiple](maths/least_common_multiple.py)
573573
* [Line Length](maths/line_length.py)
574+
* [Liouville Lambda](maths/liouville_lambda.py)
574575
* [Lucas Lehmer Primality Test](maths/lucas_lehmer_primality_test.py)
575576
* [Lucas Series](maths/lucas_series.py)
576577
* [Maclaurin Series](maths/maclaurin_series.py)

bit_manipulation/count_1s_brian_kernighan_method.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ def get_1s_count(number: int) -> int:
1717
>>> get_1s_count(-1)
1818
Traceback (most recent call last):
1919
...
20-
ValueError: the value of input must be positive
20+
ValueError: Input must be a non-negative integer
2121
>>> get_1s_count(0.8)
2222
Traceback (most recent call last):
2323
...
24-
TypeError: Input value must be an 'int' type
24+
ValueError: Input must be a non-negative integer
25+
>>> get_1s_count("25")
26+
Traceback (most recent call last):
27+
...
28+
ValueError: Input must be a non-negative integer
2529
"""
26-
if number < 0:
27-
raise ValueError("the value of input must be positive")
28-
elif isinstance(number, float):
29-
raise TypeError("Input value must be an 'int' type")
30+
if not isinstance(number, int) or number < 0:
31+
raise ValueError("Input must be a non-negative integer")
32+
3033
count = 0
3134
while number:
3235
# This way we arrive at next set bit (next 1) instead of looping

0 commit comments

Comments
 (0)