Created
November 29, 2022 03:03
-
-
Save Per48edjes/5b7172d042ccd614f20dbd190f052fa1 to your computer and use it in GitHub Desktop.
Leetcode 202. Happy Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isHappy(n: int) -> bool: | |
""" | |
Leetcode 202. Happy number | |
>>> isHappy(7) | |
True | |
>>> isHappy(19) | |
True | |
>>> isHappy(2) | |
False | |
""" | |
num_lookup = set() | |
def sum_squares_digits(n: int) -> int: | |
s = 0 | |
while n: | |
n, d = n // 10, n % 10 | |
s += d ** 2 | |
return s | |
def happy_finder(n: int) -> bool: | |
if n in num_lookup: | |
return False | |
if (s := sum_squares_digits(n)) == 1: | |
return True | |
num_lookup.add(n) | |
return happy_finder(s) | |
return happy_finder(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment