palindrome.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from itertools import product


def is_palindrome(s):
    return s[::-1] == s


def palindromes(max_factor, min_factor):
    return [
        (a * b, {a, b})
        for (a, b) in product(range(min_factor, max_factor+1), repeat=2)
        if is_palindrome(str(a*b))
    ]


def largest_palindrome(max_factor, min_factor=0):
    return max(palindromes(max_factor, min_factor))


def smallest_palindrome(max_factor, min_factor=0):
    return min(palindromes(max_factor, min_factor))

@cds-amal thinks this looks great

Comments

This isn't as speedy as I'd like. I couldn't figure out a way to iterate over the pairs so the smallest or largest would be discovered first. :(

helenst commented 19 May 2016 at 11:49 UTC

You're not logged in right now. Please login via GitHub to comment