如何从排列组合问题中获得正确答案?

huangapple go评论74阅读模式
英文:

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

问题

Here's the translated portion of your text:

给定以下输入,我想要实现一个函数,用于计算可能正确答案的数量,但出于计算原因,不计算每个答案。我希望该函数看起来类似于这样:

from itertools import combinations_with_replacement
from itertools import permutations

def get_combinations(event, population):    
    # "计算答案的数量"
    return nr_correct_answers

以下是我希望该函数如何运行,分别给出了两个示例参数事件和一个人口:

"事件" 参数:

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

"人口" 参数:

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

预期行为(期望的返回值):

事件 1 的解空间:

[(红色, 1), (蓝色, 1), (黑色, 1)] #1
[(红色, 2), (蓝色, 1), (黑色, 1)] #2
[(红色, 1), (蓝色, 2), (黑色, 1)] #3
[(红色, 2), (蓝色, 2), (黑色, 1)] #4
[(红色, 1), (蓝色, 1), (黑色, 2)] #5
[(红色, 2), (蓝色, 1), (黑色, 2)] #6
[(红色, 1), (蓝色, 2), (黑色, 2)] #7
[(红色, 2), (蓝色, 2), (黑色, 2)] #8

期望输出:8

事件 2 的解空间:

[(红色, 1), (红色, 2), (蓝色, 1)] #1
[(红色, 1), (红色, 2), (蓝色, 2)] #2

期望输出:2

基于事件和有编号的球的人口,我们如何确定事件_1 的 nr_correct_answers 值为 8,事件_2 的 nr_correct_answers 值为 2?

附加信息:

我不关心顺序,但是人口中的每个值都是 "唯一的",所以想象它就像是有编号和有颜色的球,但是对于解的结构,我只关心颜色。

我希望以这样的方式计算,即事件参数的结构和人口的结构都被使用,例如:

event_1 = [(1, "红色"), (1, "蓝色"), (1, "黑色")]
event_2 = [(2, "红色"), (1, "蓝色")]
population = [(2, "红色"), (2, "蓝色"), (2, "黑色")]

对于事件_1 和人口,函数的预期行为:

def get_combinations(event, population):

    # 使用事件 1 中的参数 1、1 和 1,使用人口中的参数 2、2 和 2
    # 计算答案的数量

    return nr_correct_answers

我已尝试了 itertools.permutations 和 itertools.product 函数,但没有成功!

英文:

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!

答案1

得分: 2

以下是翻译好的内容:

你需要一个 "n 选 k" 函数,Python 中有内置的 math.comb 函数可以使用。结果是所有 "n 选 k" 的乘积,其中 k 是事件中每个唯一项目的出现次数,n 是相同项目在总体中的出现次数。

至于事件 1:
以任何方式组合以下内容:

  • 从 2 个红色 (事件) 中选择一个的方式 (总体中有 2 个红色): 2
  • 从 2 个蓝色 (事件) 中选择一个的方式 (总体中有 2 个蓝色): 2
  • 从 2 个黑色 (事件) 中选择一个的方式 (总体中有 2 个黑色): 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
英文:

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

huangapple
  • 本文由 发表于 2023年6月15日 15:56:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定