Advertisement
here2share

# reverseKthCombination.py

Apr 13th, 2024 (edited)
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. # reverseKthCombination.py
  2.  
  3. def nCr(n, r):
  4.     r = min(r, n-r)
  5.     if r == 0: return 1
  6.     res = 1
  7.     for k in range(1, r+1):
  8.         res = res*(n-k+1)//k
  9.     return res
  10.  
  11. def reverseKthCombination(k, l, r):
  12.     if len(l) == r:
  13.         return l
  14.     else:
  15.         i=nCr(len(l)-1, r)
  16.         if k < i:
  17.             return reverseKthCombination(k, l[1:], r)
  18.         else:
  19.             return l[0:1] + reverseKthCombination(k-i, l[1:], r-1)
  20.  
  21. test = [4, 6, 2, 1, 9, 0]
  22.  
  23. total = nCr(len(test), 3)
  24. for k in range(total):
  25.     print(reverseKthCombination(k, test, 3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement