Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Last active June 10, 2016 16:20
Show Gist options
  • Save jrjames83/80c5a389b82a25638973485bdd98e363 to your computer and use it in GitHub Desktop.
Save jrjames83/80c5a389b82a25638973485bdd98e363 to your computer and use it in GitHub Desktop.
Largest palindrome product (python euler4)
#https://projecteuler.net/problem=4
#https://projecteuler.net/problem=4
def isPal(string):
if len(string) <= 1:
return True
if list(string)[0] != list(string)[-1]:
return False
x = list(string)
if x[0] == x[-1]:
x.pop(0)
x.pop(-1)
return isPal(x)
combos = []
for num in range(100,1000):
for num1 in range(100,1000):
combos.append((num, num1))
#products = [(tup[0] * tup[1]) for tup in combos]
#print max([nbr for nbr in products if isPal(str(nbr))])
#Make it a 1 liner
print max([nbr for nbr in [(tup[0] * tup[1]) for tup in combos] if isPal(str(nbr))])
@jrjames83
Copy link
Author


#https://projecteuler.net/problem=4
def isPal(string):
    if len(string) <= 1:
        return True

    if list(string)[0] != list(string)[-1]:
        return False

    x = list(string)
    if x[0] == x[-1]:
        x.pop(0)
        x.pop(-1)
        return isPal(x)


combos = []
for num in range(100,1000):
    for num1 in range(100,1000):
        combos.append((num, num1))

#products = [(tup[0] * tup[1]) for tup in combos]
#print max([nbr for nbr in products if isPal(str(nbr))])

#Make it a 1 liner
print max([nbr for nbr in [(tup[0] * tup[1]) for tup in combos] if isPal(str(nbr))])














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