From a25c53e8b0cc73ff718ec406ac04cca0c2ddbb02 Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Sun, 20 Nov 2022 14:59:25 +0400 Subject: [PATCH] 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> --- DIRECTORY.md | 1 + .../count_1s_brian_kernighan_method.py | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index e2fffec57..83da4b76a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/bit_manipulation/count_1s_brian_kernighan_method.py b/bit_manipulation/count_1s_brian_kernighan_method.py index e6d6d6534..2ed81b09d 100644 --- a/bit_manipulation/count_1s_brian_kernighan_method.py +++ b/bit_manipulation/count_1s_brian_kernighan_method.py @@ -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