Can anyone give me any "clues" to speed up my solution(s) for palin as they all timeout, do I need to change the way input is gathered or the way I calculate the palindromes ?
One example of a slow solution:
!/usr/local/bin/python
import sys
def calc(arg):
nines = {'99':'101','999':'1001','9999':'10001','99999':'100001','999999':'1000001'}
if arg in nines: print nines[arg]
if int(arg) < 11:
print "11"
else:
ls = []
for i in arg:ls.append(i)
le = len(ls)
sm = int(arg)
if le == 2:
ls[1] = ls[0]
if int(''.join(ls)) <= sm:
ls[0] = str(int(ls[0])+1)
ls[1] = ls[0]
print ''.join(ls)
if le == 3:
ls[2] = ls[0]
if int(''.join(ls)) <= sm:
ls[1] = str(int(ls[1])+1)
print ''.join(ls)
if le == 4:
ls[3] = ls[0]
ls[2] = ls[1]
if int(''.join(ls)) <= sm:
ls[1] = str(int(ls[1])+1)
ls[2] = str(int(ls[2])+1)
print ''.join(ls)
if le == 5:
ls[4] = ls[0]
ls[3] = ls[1]
if int(''.join(ls)) <= sm:
ls[2] = str(int(ls[2])+1)
print ''.join(ls)
if le == 6:
ls[5] = ls[0]
ls[4] = ls[1]
ls[3] = ls[2]
if int(''.join(ls)) <= sm:
ls[2] = str(int(ls[2])+1)
ls[3] = str(int(ls[3])+1)
print ''.join(ls)
iter = sys.stdin.readline().strip()
args = []
for i in xrange(0,int(iter)):
args.append(sys.stdin.readline().strip())
map(calc,args)
sys.exit(0)
I've also tried for loops, array.array as solutions, with psyco, all too slow .
Any tips on how to speed up python solutions would be good ...
Cheers 