Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # reverseKthCombination.py
- def nCr(n, r):
- r = min(r, n-r)
- if r == 0: return 1
- res = 1
- for k in range(1, r+1):
- res = res*(n-k+1)//k
- return res
- def reverseKthCombination(k, l, r):
- if len(l) == r:
- return l
- else:
- i=nCr(len(l)-1, r)
- if k < i:
- return reverseKthCombination(k, l[1:], r)
- else:
- return l[0:1] + reverseKthCombination(k-i, l[1:], r-1)
- test = [4, 6, 2, 1, 9, 0]
- total = nCr(len(test), 3)
- for k in range(total):
- print(reverseKthCombination(k, test, 3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement