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