Skip to content

Instantly share code, notes, and snippets.

@yuheiomori
Last active October 4, 2015 14:57
Show Gist options
  • Save yuheiomori/2656552 to your computer and use it in GitHub Desktop.
Save yuheiomori/2656552 to your computer and use it in GitHub Desktop.
CodeEval Happy Numbers
import sys
def is_happy_number(n, mem=None):
if mem is None:
mem = []
candidate = sum([int(i) ** 2 for i in n])
if candidate == 1:
return 1
elif candidate in mem:
return 0
else:
mem.append(candidate)
return is_happy_number(str(candidate), mem)
if __name__ == '__main__':
test_cases = open(sys.argv[1], 'r')
for line in test_cases:
print is_happy_number(line.strip())
test_cases.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment