Python LSTBTF: Unfortunately no help from anyone to get over the TLE issue?

Hi all,

I tried running the following code in python3.6
PYTHON-3.6: https://www.codechef.com/viewsolution/27872134

It gave TLE for two test cases. One friend suggested to run the same code in pypy-3. But in that i am getting RE(SEGV). Any help would be appreciated!

PYPY3: https://www.codechef.com/viewsolution/27874447

There are at least two problems here.

  1. Recursion is slow. This probably is enough to give you the TLE. It isn’t too bad to replace it with a while loop with the same logic.

  2. Print is slow. Also probably enough for a TLE on the biggest test cases. You should only use it once to output the entire result rather than 1000000 times if one of the cases was 1000000. There are two simple ways to make something to output by concatenating a list L of integers:

out = ''.join(map(str,L))

Alternatively, the slightly less fancy

def ltoi(lis):
    res=0
    for x in lis:
        res*=10
        res+=x
    return res

out = str(ltoi(L))
1 Like

@coulson72, Thanks a lot.

using out = β€˜β€™.join(map(str,L)) helped me to get over the TLE issues.