Problem p02744 (python)

Fixed submission:
n = int(input())

base = ['a' for i in range(n)]

end = ['z' for i in range(n)]

def sorted_k_partitions(seq, k):
    n = len(seq)
    groups = []  # a list of lists, currently empty

    def generate_partitions(i):
        if i >= n:
            yield list(map(tuple, groups))
        else:
            if n - i > k - len(groups):
                for group in groups:
                    group.append(seq[i])
                    yield from generate_partitions(i + 1)
                    group.pop()

            if len(groups) < k:
                groups.append([seq[i]])
                yield from generate_partitions(i + 1)
                groups.pop()

    result = generate_partitions(0)

    # Sort the parts in each partition in shortlex order
    result = [sorted(ps, key = lambda p: (len(p), p)) for ps in result]
    # Sort partitions by the length of each part, then lexicographically.
    result = sorted(result, key = lambda ps: tuple(map(len, ps)) + (ps,))

    return result

seq = [i+1 for i in range(n)]

# print(seq)

res = []

for k in seq:
    for groups in sorted_k_partitions(seq, k):
        temp = ['a' for i in range(n)]
        tmp = []
        tmp = list(groups)
        tmp.sort()
        # print(tmp)
        counter = 0
        for t in tmp:
            for e in t:
                temp[e-1] = chr(ord('a') + counter)
            counter += 1
        # print('after')
        # print(temp)
        res.append(''.join(temp))

res.sort()

for r in res:
    print(r)

Buggy submission (verdict: Runtime Error):
n = int(input())

base = ['a' for i in range(n)]

end = ['z' for i in range(n)]

def sorted_k_partitions(seq, k):
    """Returns a list of all unique k-partitions of `seq`.

    Each partition is a list of parts, and each part is a tuple.

    The parts in each individual partition will be sorted in shortlex
    order (i.e., by length first, then lexicographically).

    The overall list of partitions will then be sorted by the length
    of their first part, the length of their second part, ...,
    the length of their last part, and then lexicographically.
    """
    n = len(seq)
    groups = []  # a list of lists, currently empty

    def generate_partitions(i):
        if i >= n:
            yield list(map(tuple, groups))
        else:
            if n - i > k - len(groups):
                for group in groups:
                    group.append(seq[i])
                    yield from generate_partitions(i + 1)
                    group.pop()

            if len(groups) < k:
                groups.append([seq[i]])
                yield from generate_partitions(i + 1)
                groups.pop()

    result = generate_partitions(0)

    # Sort the parts in each partition in shortlex order
    result = [sorted(ps, key = lambda p: (len(p), p)) for ps in result]
    # Sort partitions by the length of each part, then lexicographically.
    result = sorted(result, key = lambda ps: (*map(len, ps), ps))

    return result

seq = [i+1 for i in range(n)]

# print(seq)

res = []

for k in seq:
    for groups in sorted_k_partitions(seq, k):
        temp = ['a' for i in range(n)]
        tmp = []
        tmp = list(groups)
        tmp.sort()
        # print(tmp)
        counter = 0
        for t in tmp:
            for e in t:
                temp[e-1] = chr(ord('a') + counter)
            counter += 1
        # print('after')
        # print(temp)
        res.append(''.join(temp))

res.sort()

for r in res:
    print(r)