I need an algorithm which rank/unrank a combination with duplicates. What i need is for n numbers choose k numbers with upto r duplicates.
for example for the list with n(range) = 4, k(len_combination) = 3, r(max_repeats) =2
(1, 1, 2)
(1, 1, 3)
(1, 1, 4)
(1, 2, 2)
(1, 2, 3)
(1, 2, 4)
(1, 3, 3)
(1, 3, 4)
(1, 4, 4)
(2, 2, 3)
(2, 2, 4)
(2, 3, 3)
(2, 3, 4)
(2, 4, 4)
(3, 3, 4)
(3, 4, 4)
I would like the output as a the rank of the combination with (1,1,2) ranking lowest this link provides me with an algorithm for permutations with duplicates and this wiki page gives me a method to calculate combinatorial rank without dupicates, my requirement is combinatorial rank with limited duplicates
I am aware of itertools.product,Counting where my sequence is in the full list is not an option as I am dealing with n>100 what I need is a function like this. I am looking for a combinatoric calculation based solution.
def rank_seq(S,n,k,r):
##some kickass math calculations
return rank
print(rank_seq(2,2,4),4,3,2)) ##output 11
print(rank_seq(2,2,4),5,3,2)) ##output 16
print(rank_seq(2,2,4),6,3,2)) ##output 22