Skip to content

Instantly share code, notes, and snippets.

@emadshaaban92
Last active August 29, 2015 13:56
Show Gist options
  • Save emadshaaban92/9043654 to your computer and use it in GitHub Desktop.
Save emadshaaban92/9043654 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import heapq
from itertools import imap, ifilter
import time
before = time.time()
class Pair(object):
def __init__(self, f1, f2):
self.f1, self.f2 = f1, f2
self.total = f1 + f2
def __eq__(self, other):
return self.total == other.total
def __lt__(self, other):
return self.total > other.total
def __le__(self, other):
return self.total >= other.total
def pair_with(elem, it):
return (Pair(elem,el) for el in it)
pairs_generators = [pair_with(n, xrange(n,100,-1)) for n in xrange(999,100,-1)]
sorted_combs = heapq.merge(*pairs_generators)
products = imap(lambda p: p.f1 * p.f2, sorted_combs)
palindrome_products = ifilter(lambda n : n == ''.join(reversed(n)), imap(str,products))
print next(palindrome_products)
print time.time()-before
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment