I absolutely cannot figure out how to decrease it's run time... I know type casts are slow, but I don't think I can remove any more. I even optimized finding the length of the integers.
import math
def align(num):
num2 = str(num)[::-1]
newNum = int(str(num)[0:(math.floor(((int(math.log10(num))+1)+1)/2))] + num2[-(math.floor((len(num2)/2))):])
return newNum
matches = {1: 10, 2: 11, 3: 10, 4: 110, 5: 100, 6: 1100, 7: 1000, 8: 11000, 9: 10000}
try:
numLines = int(input())
except:
numLines = 0
for x in range(0, numLines):
length = 0
try:
num = int(input())
except:
continue
newNum = align(num)
length = int(math.log10(newNum))+1
while(newNum <= num):
currLength = int(math.log10(newNum))+1
try:
newNum = newNum + matches[currLength]
except:
break
if(length < currLength):
newNum = align(newNum)
length = length + 1
print(newNum)
Suggestions? I've already spent too long on what is not an overly difficult problem. Thanks in advance!