Hong-Phuc Bui
2024-04-29 931d213d4395c6d0ef93f30fe606da8ee3baa6d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#! /usr/bin/env python
import sys
 
n = int(sys.argv[1])
 
# permutations = [[0]]
# element = 1
# n = 2
#
# next_permutations = []
# for p in permutations:
#     for position in range(0, len(p) + 1):
#         new_permutation = p.copy()
#         new_permutation.insert(position, element)
#         next_permutations.append(new_permutation)
# print(next_permutations)
 
permutations = [[]]
element = n - 1
 
while element >= 0:
    next_permutations = []
    for p in permutations:
        for position in range(len(p), -1, -1):
            new_permutation = p.copy()
            new_permutation.insert(position, element)
            next_permutations.append(new_permutation)
    permutations = next_permutations
    element -= 1
 
print(permutations)