Skip to content

Commit

Permalink
Merge pull request #14 from sjakobi/palindrome-products
Browse files Browse the repository at this point in the history
New exercise: python palindrome-products
  • Loading branch information
kytrinyx committed Mar 22, 2014
2 parents 2f0555a + 26d8544 commit 46a6846
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions EXERCISES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sum-of-multiples
space-age
grains
gigasecond
palindrome-products
triangle
scrabble-score
sieve
Expand Down
18 changes: 18 additions & 0 deletions palindrome-products/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def largest_palindrome(max_factor, min_factor=0):
return max(palindromes(max_factor, min_factor), key=lambda tup: tup[0])


def smallest_palindrome(max_factor, min_factor):
return min(palindromes(max_factor, min_factor), key=lambda tup: tup[0])


def palindromes(max_factor, min_factor):
return ((a * b, (a, b))
for a in range(min_factor, max_factor + 1)
for b in range(min_factor, a + 1)
if is_palindrome(a * b))


def is_palindrome(n):
s = str(n)
return s == s[::-1]
50 changes: 50 additions & 0 deletions palindrome-products/palindrome_products_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Notes regarding the implementation of smallest_palindrome and
largest_palindrome:
Both functions must take two keyword arguments:
max_factor -- int
min_factor -- int, default 0
Their return value must be a tuple (value, factors) where value is the
palindrome itself, and factors is an iterable containing both factors of the
palindrome in arbitrary order.
"""

try:
from palindrome import smallest_palindrome, largest_palindrome
except ImportError:
raise SystemExit('Could not find palindrome.py. Does it exist?')

import unittest


class PalindromesTests(unittest.TestCase):
def test_largest_palindrome_from_single_digit_factors(self):
value, factors = largest_palindrome(max_factor=9)
self.assertEqual(9, value)
self.assertIn(set(factors), [{1, 9}, {3, 3}])

def test_largest_palindrome_from_double_digit_factors(self):
value, factors = largest_palindrome(max_factor=99, min_factor=10)
self.assertEqual(9009, value)
self.assertEqual({91, 99}, set(factors))

def test_smallest_palindrome_from_double_digit_factors(self):
value, factors = smallest_palindrome(max_factor=99, min_factor=10)
self.assertEqual(121, value)
self.assertEqual({11}, set(factors))

def test_largest_palindrome_from_triple_digit_factors(self):
value, factors = largest_palindrome(max_factor=999, min_factor=100)
self.assertEqual(906609, value)
self.assertEqual({913, 993}, set(factors))

def test_smallest_palindrome_from_triple_digit_factors(self):
value, factors = smallest_palindrome(max_factor=999, min_factor=100)
self.assertEqual(10201, value)
self.assertEqual({101, 101}, set(factors))


if __name__ == '__main__':
unittest.main()

0 comments on commit 46a6846

Please sign in to comment.