#! /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)