Skip to content

Instantly share code, notes, and snippets.

@Per48edjes
Created November 29, 2022 03:03
Show Gist options
  • Save Per48edjes/5b7172d042ccd614f20dbd190f052fa1 to your computer and use it in GitHub Desktop.
Save Per48edjes/5b7172d042ccd614f20dbd190f052fa1 to your computer and use it in GitHub Desktop.
Leetcode 202. Happy Number
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