Last active
October 4, 2015 14:57
-
-
Save yuheiomori/2656552 to your computer and use it in GitHub Desktop.
CodeEval Happy Numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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