Skip to content

Instantly share code, notes, and snippets.

@imiric
Last active December 16, 2015 14:29
Show Gist options
  • Save imiric/5448696 to your computer and use it in GitHub Desktop.
Save imiric/5448696 to your computer and use it in GitHub Desktop.
Finds palindrome numbers within a list of ranges. Input file here: https://gist.github.com/hminaya/5435673
#!/usr/bin/env pypy
def main():
palindromes = []
ranges = [[int(i) for i in line.split()] for line in open('seed.txt')]
ranges_flat = [i for r in ranges for i in r]
lo, hi = min(ranges_flat), max(ranges_flat)
while hi >= lo:
n = str(lo)
if n == n[::-1]:
palindromes.append(lo)
lo += 1
total = 0
for lo, hi in ranges:
for p in palindromes:
if lo <= p <= hi:
total += 1
else:
continue
print 'TOTAL: %d' % total
if __name__ =='__main__':
main()
$ pypy --version
Python 2.7.3 (17df5567c9a0, Apr 15 2013, 03:35:20)
[PyPy 2.0.0-beta2 with GCC 4.8.0]
$ time pypy capicua2.py
TOTAL: 106814
pypy capicua2.py 0.37s user 0.01s system 99% cpu 0.388 total
@ajpinedam
Copy link

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment