-
Notifications
You must be signed in to change notification settings - Fork 0
/
145_reversible_number.py
36 lines (33 loc) · 1.48 KB
/
145_reversible_number.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
max_len = 3
def rec(digits):
l = len(digits)
if l == 0:
return [l for i in range(1, 10) for l in rec([i])]
elif l >= max_len:
return [digits]
elif l <= max_len / 2:
return [l for i in range(10) for l in rec(digits + [i])]
elif l == max_len / 2 + 1:
if digits[-1] + digits[max_len-l] > 9:
if digits[max_len-l-1] % 2 == 0:
return [l for i in range(max(10-digits[-2], 1 if l == max_len-1 else 0), 10, 2) for l in rec(digits + [i])]
else:
return [l for i in range(max(10-digits[-2], 1), 10, 2) for l in rec(digits + [i])]
else:
if digits[max_len-l-1] % 2 == 0:
return [l for i in range(max(10-digits[-2], 1), 10, 2) for l in rec(digits + [i])]
else:
return [l for i in range(max(10-digits[-2], 1 if l == max_len-1 else 0), 10, 2) for l in rec(digits + [i])]
else:
if digits[-1] + digits[max_len-l] > 9:
if digits[max_len-l-1] % 2 == 0:
return [l for i in range(1 if l == max_len-1 else 0, 10, 2) for l in rec(digits + [i])]
else:
return [l for i in range(1, 10, 2) for l in rec(digits + [i])]
else:
if digits[max_len-l-1] % 2 == 0:
return [l for i in range(1, 10, 2) for l in rec(digits + [i])]
else:
return [l for i in range(1 if l == max_len-1 else 0, 10, 2) for l in rec(digits + [i])]
r = rec([])
print len(r), r