Skip to content

Updated happy_number.py #11831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions maths/special_numbers/happy_number.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def is_happy_number(number: int) -> bool:
"""
A happy number is a number which eventually reaches 1 when replaced by the sum of
the square of each digit.
the squares of its digits.

:param number: The number to check for happiness.
:return: True if the number is a happy number, False otherwise.
Expand All @@ -17,23 +17,22 @@
>>> is_happy_number(0)
Traceback (most recent call last):
...
ValueError: number=0 must be a positive integer
ValueError: Input must be a positive integer, got 0.
>>> is_happy_number(-19)
Traceback (most recent call last):
...
ValueError: number=-19 must be a positive integer
ValueError: Input must be a positive integer, got -19.
>>> is_happy_number(19.1)
Traceback (most recent call last):
...
ValueError: number=19.1 must be a positive integer
ValueError: Input must be a positive integer, got 19.1.
>>> is_happy_number("happy")
Traceback (most recent call last):
...
ValueError: number='happy' must be a positive integer
ValueError: Input must be a positive integer, got 'happy'.
"""
if not isinstance(number, int) or number <= 0:
msg = f"{number=} must be a positive integer"
raise ValueError(msg)
raise ValueError(f"Input must be a positive integer, got {number}.")

Check failure on line 35 in maths/special_numbers/happy_number.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (EM102)

maths/special_numbers/happy_number.py:35:26: EM102 Exception must not use an f-string literal, assign to variable first

seen = set()
while number != 1 and number not in seen:
Expand Down
Loading