As some of you know, I have made commands to try to mimic Python itertools. However, there’s only one thing that is noticeable which is missing, and that is partial permutation. There’s not much codes out there, so this remains unsolved.
This is what a partial permutation looks like:
perm = permutations(7, 3)
(0, 1, 2)
(0, 1, 3)
(0, 1, 4)
(0, 1, 5)
(0, 1, 6)
(0, 2, 1)
(0, 2, 3)
(0, 2, 4)
(0, 2, 5)
(0, 2, 6)
(0, 3, 1)
(0, 3, 2)
(0, 3, 4)
(0, 3, 5)
(0, 3, 6)
(0, 4, 1)
(0, 4, 2)
(0, 4, 3)
(0, 4, 5)
(0, 4, 6)
(0, 5, 1)
(0, 5, 2)
(0, 5, 3)
(0, 5, 4)
(0, 5, 6)
(0, 6, 1)
(0, 6, 2)
(0, 6, 3)
(0, 6, 4)
(0, 6, 5)
(1, 0, 2)
(1, 0, 3)
(1, 0, 4)
(1, 0, 5)
(1, 0, 6)
(1, 2, 0)
(1, 2, 3)
(1, 2, 4)
(1, 2, 5)
(1, 2, 6)
(1, 3, 0)
(1, 3, 2)
(1, 3, 4)
(1, 3, 5)
(1, 3, 6)
(1, 4, 0)
(1, 4, 2)
(1, 4, 3)
(1, 4, 5)
(1, 4, 6)
(1, 5, 0)
(1, 5, 2)
(1, 5, 3)
(1, 5, 4)
(1, 5, 6)
(1, 6, 0)
(1, 6, 2)
(1, 6, 3)
(1, 6, 4)
(1, 6, 5)
(2, 0, 1)
(2, 0, 3)
(2, 0, 4)
(2, 0, 5)
(2, 0, 6)
(2, 1, 0)
(2, 1, 3)
(2, 1, 4)
(2, 1, 5)
(2, 1, 6)
(2, 3, 0)
(2, 3, 1)
(2, 3, 4)
(2, 3, 5)
(2, 3, 6)
(2, 4, 0)
(2, 4, 1)
(2, 4, 3)
(2, 4, 5)
(2, 4, 6)
(2, 5, 0)
(2, 5, 1)
(2, 5, 3)
(2, 5, 4)
(2, 5, 6)
(2, 6, 0)
(2, 6, 1)
(2, 6, 3)
(2, 6, 4)
(2, 6, 5)
(3, 0, 1)
(3, 0, 2)
(3, 0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
(6, 3, 0)
(6, 3, 1)
(6, 3, 2)
(6, 3, 4)
(6, 3, 5)
(6, 4, 0)
(6, 4, 1)
(6, 4, 2)
(6, 4, 3)
(6, 4, 5)
(6, 5, 0)
(6, 5, 1)
(6, 5, 2)
(6, 5, 3)
(6, 5, 4)
perm = permutations(4, 2)
(0, 1)
(0, 2)
(0, 3)
(1, 0)
(1, 2)
(1, 3)
(2, 0)
(2, 1)
(2, 3)
(3, 0)
(3, 1)
(3, 2)
Contrast that with my full permutations command:
rep_permutations 7
With the above code, you get 5040 permutations.
And the kicker? I have made tools to extract the location from the number, and to extract number from a location. It’s O(n) in terms of finding them as they don’t involve generating other permutations. If I have to generate other permutations, then it would be O(n!).
#@cli rep_extract_permutation_order: number_of_item,permutation_number
#@cli : Return permutation order at index permutation_number.
#@cli rep_find_permutation_lexicographic_index: number_list
#@cli : Return the lexicographic index in which this permutation is found within a list of all possible permutation.
#@cli :
#@cli : number_list must be in the form of 0,1,2,3.... , but can be in any order.
Here’s the result of those:
$ rep_permutations 7 echo {I[#-1,3000]} rm.
[gmic]-0./ Start G'MIC interpreter.
4,1,0,2,3,5,6
[gmic]-1./ Remove image [0] (0 images left).
[gmic]-0./ End G'MIC interpreter
$ echo ${rep_extract_permutation_order\ 7,3000}
4,1,0,2,3,5,6
$ echo ${rep_find_permutation_lexicographic_index\ 4,1,0,2,3,5,6}
3000
As you can see in the results, it works really well. It always do.