Fastest method to generate big random string with lower Latin letters

后端 未结 8 1881
难免孤独
难免孤独 2020-12-06 05:38

I\'m trying to solve this problem from Timus Online Judge. To solve this problem you need generate a sequence of 1 000 000 lowercase Latin letters and write it to stdin in

相关标签:
8条回答
  • 2020-12-06 06:03

    execution time 0.51s

    from sys import stdout
    from string import ascii_lowercase
    l = 1000000
    q = ['a']*l
    lc = list(ascii_lowercase)
    c = 0
    for i in range(0,l-2,3):
        j = i // 3
        j_26 = j // 26
        q[i]= lc[j_26 // 26 % 26]
        q[i+1] = lc[j_26 % 26]
        q[i+2] = lc[j % 26]
    
    stdout.write(''.join(q))
    
    0 讨论(0)
  • 2020-12-06 06:05

    Try turning some part of it into C++ or another compiled language. That will almost guaranteed make it faster. Python, unfortunately, isn't too fast, especially when it comes to things like this. Try C++, C, or Pascal.

    EDIT: Also see the Python Performance Tips

    0 讨论(0)
提交回复
热议问题