1 / 6
Jun 2014

When I submit this code,it gives me time limit exceeded error. Please help me fix it.

def palin(x):
    list = [int(x) for x in range(x+1,x+1000,1)]
for item in list:
	a = str(item)
	rev = ""
	for letter in a:
		rev = letter + rev
		if rev == a:
			return a
		else:
			pass
N = int(raw_input())
while N:
	N -= 1
	k = int(raw_input())
	print palin(k)
  • created

    Jun '14
  • last reply

    Jul '14
  • 5

    replies

  • 515

    views

  • 6

    users

I'm getting a TLE error for mine as well. I believe yours will fail even if was fast enough. Doesn't the output have to be grouped together?
In any case my program separated the input into a list of characters and I had an algorithm go through the process of changing the digits only if they needed to be changed. And that was too slow so your way is going to be way way too slow.
I don't have the solution yet but I'm thinking maybe i need to just keep the input as a number and not do any int <-> str conversions. I believe that's slowing things down.
Just as a general recommendation don't use brute force to solve these problems with python it's just too slow.

10 days later

I made it in Python 2.7, but took almost 3 seconds dealing only with string manipulation.
Maybe long integers are faster.

23 days later

After several attempts, I was finally able to get AC as well but my time leaves something to be desired on this one for sure - about 4.5 seconds.

I really had to think of a lot of corner/edge cases so it was a good exercise.

I originally was getting quite a few TLE but I think (...?) that was because of the str-to-int conversions and calculations I was unwisely doing on potentially huge numbers (10^6 digits).

My AC solution had a minimal number of those conversions, and then only for single digits. I wonder how much it would improve if it were possible to eliminate ALL conversions and implement a pure string-only solution.

I think the next things I will do will be to:

-> experiment with large integer conversion and calculation
-> investigate eliminating all conversions
-> revisit my algorithm (i.e. string slicing and my custom carry function)

That should be the final target ... wink

Today I noticed that my 2009 AC Python 2.5 (+psyco) submission (runtime was 0.08 s on top of the ranklist) was no longer listed.
It seems there has been a rejudge in 2013 or 2014 so that all former AC Python submissions that used psyco are no longer valid.
So I had to code a Python 3.x solution ...

Edit: Ah, okay. There has been a rejudge in June 2014 because of weak testcases (you find that in the comments below the problemset).
Unfortunately that rejudge killed also all psyco-using Python submissions. frowning

14 days later

I was surprised that all my submissions got WA after the rejudge. At the time when I developed the algo I compared the 1st 100000 against a brute force solution and was pretty sure that the algo is right. The test cases are perhaps better now, the input file isn't. raw_input() is good enough to read the lines and get a decent score anyway.