Skip to content

Instantly share code, notes, and snippets.

@KrzysztofCiba
Last active October 11, 2021 09:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrzysztofCiba/5236362 to your computer and use it in GitHub Desktop.
Save KrzysztofCiba/5236362 to your computer and use it in GitHub Desktop.
Get nth permutation of a list, while permutations are created in a lexicographical order.
#!/bin/env python
"""
Solves a problem of getting permutation of a given index,
while you know that permutations are created in a lexicographical order.
As input you are specifying a list of elements and index of permutation you want to get.
"""
## imports
from math import factorial
## this one just for checking
from itertools import permutations
def getNthPermutation( pIndex, inList, outList=[] ):
""" get :pIndex: permutation of :inList:
:warn: permutations are sorted in lexicographical order starting from 0, i.e.:
0 -> [1, 2, 3], 1 -> [1, 3, 2] and so on
:param int pIndex: given permutation index
:param list inList: initial list of elements
:param list outList: result list
"""
## permutation index too big
if pIndex >= factorial( len(inList) ): return []
## no more elements to use
if not inList: return outList
## make sure eList is sorted
inList.sort()
## get factorial
f = factorial( len(inList)-1 )
index = pIndex / f
reminder = pIndex % f
## add new element to outList
outList.append( inList[index] )
## ...and remove it from inList
del inList[index]
## bail out or call recursively depending of the reminder
if not reminder:
return outList + inList
else:
return getNthPermutation( reminder, inList, outList )
### test execution
if __name__ == "__main__":
ps = permutations( [1,2,3,4,5,6] )
for i in range(factorial(6)):
resList = []
res = getNthPermutation( i, [1,2,3,4,5,6], resList )
check = list( ps.next() )
assert( res == check )
@inv-Ayiba
Copy link

inv-Ayiba commented Sep 13, 2021

What about of the form nPr = n!/(n-r)! , Your current code is n=r ... Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment