Problem 36

completed December 6, 2011

Comments

I’ve been saving a list of the important snippets of code that I’ve been learning to write as I go. I’m glad I’ve been doing that because it came in very handle with this problem

Code

import re
def num2str(n):
	return re.split('_', re.sub('\B', '_', str(n)))
	
def ispalendrome(n):
	n = num2str(n)
	for i in range(len(n)):
		if n[i] == n[-(i+1)]: continue
		else: return 'FALSE'
	return 'TRUE'

sum = 0
for n in range(10**6):
	ten = n
	two = bin(n); two = str(two).lstrip('0b')
	if ispalendrome(ten) == 'TRUE' and ispalendrome(two) == 'TRUE':
		sum = sum + n
		print n

print sum