-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestNumericPalindrome.py
More file actions
59 lines (52 loc) · 1.88 KB
/
LargestNumericPalindrome.py
File metadata and controls
59 lines (52 loc) · 1.88 KB
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
from itertools import combinations
from operator import mul
def build_palindromes(n):
# Find frequency numbers, build a dict of pairs
number_frequency = {}
for c in set(n):
number_frequency[c] = n.count(c)
# Build palindrome iteratively, store only
# left part and center number
# pal = x1 x2 x3, ... xn , xn+1, xn, xn-1, ..., x1
left_pal = []
center = ''
# Test all numbers
# TODO : use only reverse sorted set of input numbers
for i in ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0']:
if i in number_frequency:
# If odd frequency, use as center and extremity
if number_frequency[i] % 2 == 1:
if center == '':
center = i
for j in range((number_frequency[i] - 1) / 2):
if i != '0':
left_pal.append(i)
else:
if len(left_pal) != 0:
left_pal.append(i)
# If even frequency, only at extremity
else:
for j in range((number_frequency[i] / 2)):
if i != '0':
left_pal.append(i)
else:
if len(left_pal) != 0:
left_pal.append(i)
# Build final palindrome
pal = []
for i in left_pal:
pal.append(i)
pal.append(center)
for i in left_pal[::-1]:
pal.append(i)
return int("".join(pal))
# Compute the largest palindrome from
# all the possible input product combination
def numeric_palindrome(*args):
biggest = 0
# Test all product combinations without repetition
for i in range(1, len(args)):
for c in combinations(args, i + 1):
curr = build_palindromes(str(reduce(mul, c, 1)))
biggest = curr if curr > biggest else biggest
return biggest