-
Notifications
You must be signed in to change notification settings - Fork 0
/
closest_pal.py
44 lines (44 loc) · 962 Bytes
/
closest_pal.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
# https://practice.geeksforgeeks.org/problems/closest-palindrome/0
t = int(input())
while t:
n = input()
print('n', n)
if n == n[::-1]:
print(n)
t-=1
continue
if len(n)%2==0:
s = n[:len(n)//2]
else:
s = n[:(len(n)//2)+1]
print('s', s)
p = str(int(s)+1)
q = str(int(s)-1)
print('p, q', p, q)
if(len(n)%2!=0):
r = s+(s[::-1][1:])
p = p+(p[::-1][1:])
q = q+(q[::-1][1:])
else:
r = s+s[::-1]
p = p+p[::-1]
q = q+q[::-1]
print('r, p, q', r, p, q)
d1 = abs(int(n)-int(p))
d2 = abs(int(n)-int(q))
d3 = abs(int(n)-int(r))
if(d1<d2):
if(d1<d3):
print(p)
elif(d1==d3):
print(p if p<r else r)
else:
print(r)
else:
if(d2<d3):
print(q)
elif(d2 == d3):
print(q if q<r else q)
else:
print(r)
t-=1