class Solution:
def isHappy(self, n):
list_n, l, ls, num = [n,], 0, 0, 0
while num != 1:
if l != ls:
return False
num = sum([int(i)**2 for i in list(str(n))])
list_n.append(num)
l, ls = len(list_n), len(set(list_n))
return True
Input: 7
Output: False
Expected: True
It's from Happy Number | LeetCode OJ
Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
sum(int(i)**2 for i in list(str(n)))would be better, you are building a list for no reasonlist_nhas repeated values, it will returnFalse