In the above Python program to find the permutation for a given collection of numbers, the Program is extracting all the elements one by one and then placing them in the first position and then recurring the remaining list.
The term permutation refers to a mathematical calculation of the number of ways a particular set can be arranged. A permutation is a word that describes the number of ways things can be ordered or arranged. The number of permutations on a set of n elements is given by n!. For example, there are 2! = 21 = 2 permutations of {1, 2}, namely {1, 2} and {2, 1}, and 3! = 32*1 = 6 permutations of {1, 2, 3}, namely {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2} and {3, 2, 1}.
def permutation(num):
res_perm = [[]]
for n in num:
new_perm = []
for perm in res_perm:
for i in range(len(perm)+1):
new_perm.append(perm[:i] + [n] + perm[i:])
res_perm = new_perm
return res_perm
my_perm = [1,2,3]
print("Original Collection: ",my_perm)
print("Collection of distinct numbers:\n",permutation(my_perm))
Sample Output: Original Collection: [1, 2, 3] Collection of distinct numbers: [[3, 2, 1], [2, 3, 1], [2, 1, 3], [3, 1, 2], [1, 3, 2], [1, 2, 3]]