Consider the following list of numbers: 0, 1, 2, 3
I am trying to find all permutations of the list of length 2, 3 and 4.
i.e.
(0, 1)
(0, 2)
(0, 3)
(1, 0)
(1, 2)
(1, 3)
(2, 0)
(2, 1)
(2, 3)
(3, 0)
(3, 1)
(3, 2)
(0, 1, 2)
(0, 1, 3)
(0, 2, 1)
(0, 2, 3)
(0, 3, 1)
(0, 3, 2)
(1, 0, 2)
(1, 0, 3)
(1, 2, 0)
(1, 2, 3)
(1, 3, 0)
(1, 3, 2)
(2, 0, 1)
(2, 0, 3)
(2, 1, 0)
(2, 1, 3)
(2, 3, 0)
(2, 3, 1)
(3, 0, 1)
(3, 0, 2)
(3, 1, 0)
(3, 1, 2)
(3, 2, 0)
(3, 2, 1)
(0, 1, 2, 3)
(0, 1, 3, 2)
(0, 2, 1, 3)
(0, 2, 3, 1)
(0, 3, 1, 2)
(0, 3, 2, 1)
(1, 0, 2, 3)
(1, 0, 3, 2)
(1, 2, 0, 3)
(1, 2, 3, 0)
(1, 3, 0, 2)
(1, 3, 2, 0)
(2, 0, 1, 3)
(2, 0, 3, 1)
(2, 1, 0, 3)
(2, 1, 3, 0)
(2, 3, 0, 1)
(2, 3, 1, 0)
(3, 0, 1, 2)
(3, 0, 2, 1)
(3, 1, 0, 2)
(3, 1, 2, 0)
(3, 2, 0, 1)
(3, 2, 1, 0)
I need to implement this in C, but all the algorithms I have found [1,2] only give the permutations with length equal to the length of the number list, i.e. only the results from (0, 1, 2, 3)
in the block above. Reducing the length from 4 to 3 gives only the permutations for the list 0, 1, 2
.
I can currently achieve what I want in Python using itertools.permutation
, as shown below.
import itertools
MaxN = 4
for Length in range(2, MaxN + 1):
for perm in itertools.permutations(Indices, Length):
print perm
Any advice on how to implement this in C would be greatly appreciated.
[1] http://rosettacode.org/wiki/Permutations#C
[2] http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/