Created
June 8, 2015 17:50
-
-
Save mkowoods/ae792800646721413242 to your computer and use it in GitHub Desktop.
This file contains 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
#http://www.reddit.com/r/dailyprogrammer/comments/38yy9s/20150608_challenge_218_easy_making_numbers/ | |
def get_palindromic_number(num_str, max_iter = 1000): | |
i = 0 | |
while not (num_str == num_str[::-1]) and i < max_iter: | |
num_str = str(int(num_str) + int(num_str[::-1])) | |
i += 1 | |
return (None if i == max_iter else num_str) , i | |
if __name__ == "__main__": | |
max_iter = 1000 | |
for n in ['24', '28', '11', '68', '123', '286', '196196871', '196']: | |
inp = n | |
result, i = get_palindromic_number(inp, max_iter = max_iter) | |
if result: | |
print '%s gets palindromic after %d steps: %s'%(inp, i, result) | |
else: | |
print '%s does not get palindromic after %d iterations'%(inp, max_iter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment