-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_004.py
63 lines (42 loc) · 1.52 KB
/
problem_004.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# https://projecteuler.net/problem=4
import math
import unittest
from itertools import product
def is_palidrome(n):
return str(n) == str(n)[::-1]
class TestIsPalindrome(unittest.TestCase):
def test_1_1(self):
self.assertEqual(is_palidrome(10), False)
def test_1_2(self):
self.assertEqual(is_palidrome(11), True)
def test_1_3(self):
self.assertEqual(is_palidrome(113), False)
def test_1_4(self):
self.assertEqual(is_palidrome(1), True)
def test_1_5(self):
self.assertEqual(is_palidrome(9009), True)
def test_1_6(self):
self.assertEqual(is_palidrome(111), True)
def test_1_7(self):
self.assertEqual(is_palidrome(1111), True)
def test_1_8(self):
self.assertEqual(is_palidrome(101), True)
def largest_palindrome_product():
possible_numbers = [str(ch) for ch in range(1, 10)]
palindromes = []
for n1, n2, n3 in product(possible_numbers, repeat=3):
number_1 = int(n1 + n2 + n3)
for n21, n22, n23 in product(possible_numbers, repeat=3):
number_2 = int(n21 + n22 + n23)
product_palindrome = number_1 * number_2
if is_palidrome(product_palindrome):
palindromes.append(product_palindrome)
return max(palindromes)
class TestLargestPalindromeProduct(unittest.TestCase):
'''
_ _ _ * _ _ _
'''
def test_1_1(self):
self.assertEqual(largest_palindrome_product(), 906609)
if __name__ == '__main__':
unittest.main()