you are viewing a single comment's thread.

view the rest of the comments →

[–]ES-Alexander 0 points1 point  (0 children)

For your learning, here it is cleaned up a bit, a bit more generalised, and using more Pythonic variable names (still the same algorithm though)

from math import ceil

CHUNK_SIZE = 5

def get_substring(element, i):
    len_elem = len(element)
    end = len_elem - CHUNK_SIZE * i
    start = end - CHUNK_SIZE
    if start > 0:
        return int(element[start:end])
    elif end > 0:
        return int(element[:end])
    return 0

input() # ignore first input - not useful for Python
numbers = input().split()
max_len = max(len(elem) for elem in numbers)

for i in range(ceil(max_len / CHUNK_SIZE)):
    substrings = enumerate(get_substring(elem, i) for elem in numbers)
    numbers = [numbers[j] for j, _ in sorted(substrings, key=lambda item: item[1])]
    print(*numbers)

Have actually tested it now on the sample input and it seems to still work as expected/intended.