fork(1) download
  1. #!/usr/bin/env python3
  2.  
  3. from math import factorial
  4.  
  5. def getJthPermutation(initList, j):
  6. j -= 1
  7.  
  8. result = ""
  9. fac = factorial(len(initList) - 1)
  10. for div in range(len(initList) - 1, 0, -1):
  11. k = j // fac
  12. result += initList[k]
  13. del initList[k]
  14.  
  15. j %= fac
  16.  
  17. fac //= div
  18. result += initList[0]
  19.  
  20. return result
  21.  
  22. def fct(i, j, n):
  23. list = [str(k) for k in range(n+1)]
  24.  
  25. permutation = getJthPermutation(list[:], j)
  26. print(permutation)
  27.  
  28. return permutation[i]
  29.  
  30.  
  31. if __name__ == "__main__":
  32. print(fct(3,1000000,9))
  33. print(fct(3,1,9))
  34. print(fct(1, factorial(10), 9))
  35.  
Success #stdin #stdout 0.04s 9488KB
stdin
Standard input is empty
stdout
2783915460
3
0123456789
3
9876543210
8