27 lines
627 B
Python
27 lines
627 B
Python
# Happy Number
|
|
# Repeat the process until the number equal to 1, these are happy
|
|
# OR LOOP ENDLESS in a cycle, these are not happy
|
|
|
|
|
|
class Solution:
|
|
def isHappy(self, n: int, status=None) -> bool:
|
|
if status is None:
|
|
status = []
|
|
|
|
str_n = str(n)
|
|
total = 0
|
|
for number in str_n:
|
|
total += int(number) ** 2
|
|
if total == 1:
|
|
return True
|
|
if str(total) in status:
|
|
return False
|
|
|
|
status.append(str(total))
|
|
return self.isHappy(total, status)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
number = 13
|
|
print(Solution().isHappy(number))
|