How to get the correct answers from a permutation / combination problem?

Question:

Given the following input, I would like to implement a function, that calculates the amount of possible correct answers, without calculating each answer for computational reasons. I hope the function to look something like this:

from itertools import combinations_with_replacement
from itertools import permutations

def get_combinations(event, population):    
    # "calculate number of answers"
    return nr_correct_answers

Here is how I expect the function to behave, given 2 examples for parameters event, and one population:

"event" param:

event_1 = ["Red", "Blue", "Black"]
event_2 = ["Red", "Red", "Blue"]

"population" param:

population= ["Red", "Red", "Blue", "Blue", "Black", "Black"]

Expected bahviour (expected return values):

Solution space for event 1:

[(Red,1),(Blue,1),(Black,1)] #1
[(Red,2), (Blue,1), Black,1)] #2
[(Red,1),(Blue,2),(Black,1)] #3
[(Red,2),(Blue,2), Black,1)] #4
[(Red,1),(Blue,1),(Black,2)] #5
[(Red,2),(Blue,1), Black,2)] #6
[(Red,1),(Blue,2),(Black,2)] #7
[(Red,2), (Blue,2), Black,2)] #8

Desired Output: 8

Solution space for event 2:

[(Red,1), (Red,2), (Blue,1)] #1
[(Red,1), (Red,2), (Blue,2)] #2

Desired Output: 2

Based on the events and the population of numbered balls, how can we determine the nr_correct_answers value 8 for event_1 and the nr_correct_answers value 2 for event_2?

Additional information:

I am not intersted in the order, however each value in the population is "unique", so imagine it is like a numbered and coloured ball, but for the structure of the solution I am only interested in the colour.

I would like to calculate it in such a way, that the structure the event parameter and the structure of the population are being used, so for example:

event_1= [(1,"Red"), (1,"Blue"), (1,"Black")]
event_2= [(2,"Red"), (1,"Blue")]
population= [(2,"Red"), (2,"Blue"), (2,"Black")]

Expected behaviour of the function for event_1 and population:

def get_combinations(event, population):

    # use the params 1,1 and 1 from the event 1use the params 2,2 and 2 from the population
    # calculate number of answers

    return(nr_correct_answers)

I have tried itertools perumtations and itertools product functions – but without success!

Asked By: B.abba

||

Answers:

Well, you need a "n choose k" function which python ships in math.comb. The result is the product of all "n choose k" where k are the number of occurrences for each unique item in event, and n are the numbers of occurrences of the same item in the population.

As for event 1:
Combine the following in any way:

  • the ways to pick one red (event) from 2 reds (pop): 2
  • the ways to pick one blue (event) from 2 blues (pop): 2
  • the ways to pick one black (event) from 2 blacks (pop): 2
    -> 2*2*2 = 8
from collections import Counter
from math import comb, prod

def get_combinations(event, population):    
    cp = Counter(population)        
    return prod(comb(cp[k], ve) for k, ve in Counter(event).items())

>>> get_combinations(event_2, population)
2
>>> get_combinations(event_1, population)
8
Answered By: user2390182