1 / 4
Sep 2018

My code for the given problem is
def nine(s,l):
for i in range(0,l):
if s[i]!=‘9’:
return 0
return 1
t=int(input())
while t!=0:
n=int(input())
s=str(n)
l=len(s)
if (nine(s,l)):
print(n+2)
else:
if l%2==0:
s1=s[:l//2-1]
if int(s1+s[l//2-1]+s[l//2-1]+s1[::-1])>n:
print(s1+s[l//2-1]+s[l//2-1]+s1[::-1])
else:
print(s1+str(int(s[l//2-1])+1)+str(int(s[l//2-1])+1)+s1[::-1])
else:
s1=s[:l//2]
if int(s1+s[l//2]+s1[::-1])>n:
print(s1+s[l//2]+s1[::-1])
else:
print(s1+str(int(s[l//2])+1)+s1[::-1])
t-=1

when i submit it to online judge it responds time limit exceeded but when i run it on ideone.com3 for any input it barely takes more than 0.09 sec.
please tell any mistake i have done.Thank you

  • created

    Sep '18
  • last reply

    Sep '18
  • 3

    replies

  • 726

    views

  • 2

    users

  • 2

    links

It’s going to be pretty difficult to figure out what’s happening with the code laid out like that.

I tried it with some big numbers, and for some well below 1000000 digits, it took around 0.2s. But that’s on ideone, I don’t know how their servers compare to SPOJ’s.

The problem statement doesn’t say how many test cases there are, but there could be tens, hundreds or thousands.

Looking at the code, I see several conversions between integers and string - that’s got to be relatively slow when you’re working with numbers this big. Can you do without them?