Skip to content

Instantly share code, notes, and snippets.

@ABcDexter
Created August 16, 2024 19:02
Show Gist options
  • Save ABcDexter/57cc356c3fba078e25c1c995844b2ac1 to your computer and use it in GitHub Desktop.
Save ABcDexter/57cc356c3fba078e25c1c995844b2ac1 to your computer and use it in GitHub Desktop.
SPOJ code for PALIN
#AnubhavBalodhi, SPOJ, PALIN, 16/08/2024
def nextPalin(num :str ) -> str:
odd_or_not = len(num)%2
h_len = len(num)//2
full = [int(c) for c in num]
first_half = [int(c) for c in num[:h_len]]
secnd_half = [int(c) for c in num[h_len+odd_or_not:]]
rev_first_half = list(reversed(first_half))
def makePalin() :
# iff the PALIN is of ODD length
if odd_or_not:
middle = len(num)//2
m=full[middle]
if m == 9 :
m=0
return first_half + [m] + rev_first_half
else:
return first_half + rev_first_half
i=0
palin=makePalin()
while palin <= full and i < h_len:
diff = rev_first_half[i] - secnd_half[i]
if diff > 0 :
break
k = rev_first_half[i]+1
rev_first_half[i]=k
first_half[h_len-i-1]=k
i += 1
palin = makePalin()
print(f"PALIN {first_half=} {rev_first_half=} | {secnd_half=},{rev_first_half=} | {palin=} |")
return palin
def main():
t=int(input().strip())
for i in range(t):
n = input().strip()
print(nextPalin(n))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment