I'm making a function where we input a number and we check whether the sum of square of digits would equal 1 or not. If it equals one, we print true, if it does not, we show it keeps on going in an endless cycle and print False.
My code:
class Solution:
def isHappy(self, n: int,l=[]) -> bool:
sum = 0
print(l)
while(n>0):
sum = sum + int(n%10)**2
n = n//10
if(sum==1):
print('ok')
return True
else:
if(l.count(sum)>0):
return False
else:
l.append(sum)
Solution.isHappy(self,sum,l)
#Cross verify
Saa = Solution()
st=Saa.isHappy(7)
st
The problem is that it always returns False, I've added the extra print('ok') line to check and it actually prints it at the correct iteration but still returns false.
st value on printing = false
When I execute program for 7 it prints
[82, 68, 100]
[82, 68, 100, 49]
[82, 68, 100, 49, 97]
[82, 68, 100, 49, 97, 130]
[82, 68, 100, 49, 97, 130, 10]
ok
After 10 it should return True but still returns False