Given an alphabet A = {a,b,c,d,...} of length n, I would like to get all permutations of length r (r < n).
Now I would like to number these permutations, and there should be a reverse mapping.
For example:
A = {a,b,c}, r = 2
ab -> 0
ba -> 1
ac -> 2
ca -> 3
...
How can I achieve this? I have found it for problems which are order-invariant. But I cant apply it to this situation with the order.
Is there some library doing it in python?
Presumably
aais an invalid selection. Since you are dealing from a distinct alphabet, each deal produces a non-intersecting set of permutations. Your list is counted byr! * choose(n,r)which isn!/(n-r)!orn_ror "k-permutations of n elements." The canonical way to number these is lexicographically.If I were attempting to generate these using a generic library, I would find a way to deal or choose the unrepeated
relements, then produce all permutations on that set of sizer. As far as numbering goes, if you can store the permutations lexicographically in an array you can binsearch to find the index. It is possible to reverse the mapping analytically, but realistically, whatever you're doing here is going to have to have smallr.A quick search shows Python's
itertoolsgive you two functions you need:combinationsandpermutations.