Skip to content

Instantly share code, notes, and snippets.

@munguial
Created April 2, 2020 16:41
Show Gist options
  • Save munguial/9e9af36a6469287aa8fecf8c15f97335 to your computer and use it in GitHub Desktop.
Save munguial/9e9af36a6469287aa8fecf8c15f97335 to your computer and use it in GitHub Desktop.
Day 2 - Happy number
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3284/
class Solution:
def isHappy(self, n: int) -> bool:
knownNumbers = {}
while True:
if n == 1:
return True
if n in knownNumbers:
return False
knownNumbers[n] = None
n = self.calculateNextNumber(n)
return False
def calculateNextNumber(self, n: int) -> int:
strDigits = list(str(n))
return sum((int(digit) * int(digit)) for digit in strDigits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment