1 / 4
May 2020

The code below works, and doesn’t take too long to run. I have submitted it as Python 3. Any suggestions to improve?

a = int(input())
arr= []
ans= []
for i in range(0,a):
arr.append(input())

def reverser(j):

b = ""
rev = str(j)
for k in range(0,len(rev)):
    b = b + rev[len(str(j))-1-k]
    
return(int(b))
       
#print(len(str(arr[0])))

loop = 0
for k in arr:
t = int(k)
while loop<1:
    if t==reverser(t):
        ans.append(t)
        break
    else:
        t=t+1
    
for g in ans:
print(g)
  • created

    May '20
  • last reply

    May '20
  • 3

    replies

  • 801

    views

  • 2

    users

  • 1

    link

Did you notice the maximum size of the integer? You need to come up with a better algorithm than brute force.

PALIN3

Are you saying that I should just compare against a list of all palindromes smaller than 1MM?

I’m saying you’ve misread the maximum size of the integer; it can be very big, and a brute force algorithm is never going to be fast enough.