Hong-Phuc Bui
2024-09-27 8a9933e4decb3b6a57b47b6409474bd476920c24
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
32
33
#! /usr/bin/env python
import sys
 
n = int(sys.argv[1])
"""
function permutation(set: []) -> []
    if size(set) < 2 return [set];
    result = []
    for idx, e in set:
        other_elements = set \ {e}
        other_permutations = permutation(other_elements)
        for p in other_permutations:
            result.append( [e].concat(p) )
        end
    end
    return result
end
"""
 
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)