0

here is how you generate combinations :

   from itertools import combinations_with_replacement, combinations
   list(combinations([1,2,3,4,5],r=3))
   [(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)]

Is there a way to get the Nth combination without enumerating all the previous items.

  combinations([1,2,3,4,5],r=3, nth=3)   ==>  (1,3,4)

The important thing is not to enumerate so that I can call for large numbers f.e.

  combinations(range(100),r=5, nth=3000000)   ==>  .....

I'm interested of the Algorithm/Source rather than callable function.

if it is possible at all, because I want to modify it for my needs

sten
  • 7,028
  • 9
  • 41
  • 63

0 Answers0