简体   繁体   English

确定可能的组合数量

[英]Determining the number of possible combinations

I'm trying to figure out how many possible ways there are to combine various elements form this string. 我试图弄清楚有多少种方法可以组合此字符串中的各种元素。

"{Hello|Hi|Hey} {world|earth}{!|.|?}"

Where one item (separated by a pipe/|) is selected at random from each group ({}) and combined into a single string. 从每个组({})中随机选择一项(用竖线/ |分隔),并组合成一个字符串。

So the above "template" could produce: 因此,上述“模板”可能会产生:

Hello world.
Hi earth?
Hey world.
Hi world?

I'm guessing this is a type of permutation, but I want to make sure I'm getting this right. 我猜这是一种排列,但我想确保自己做对了。

It would be really nice if this worked with "n" nested items as well. 如果这也适用于“ n”个嵌套项目,那将是非常好的。

"{{Hello|Hi|Hey} {world|earth}|{Goodbye|farewell} {noobs|n3wbz|n00blets}}"

I'd prefer a math/statistics based solution over brute-force looping to get the answer if possible. 我更喜欢基于数学/统计的解决方案,而不是蛮力循环,以获取可能的答案。

Thanks! 谢谢!

Well, there are 3 x 2 x 3 = 18 combinations in your first example. 好吧,第一个示例中有3 x 2 x 3 = 18个组合。

Your second example is 3 x 4 x 2 x 3 = 72 combinations. 您的第二个示例是3 x 4 x 2 x 3 = 72个组合。

I'm not entirely sure what you mean by {a|b}|{c|d} though, I'm assuming you mean pick one of either (a or b) or (c or d), which is 4 choices. 不过,我不太确定{a|b}|{c|d}是什么意思,我假设您的意思是选择(a或b)或(c或d)之一,这是4个选择。

You might want to read up on combinations here or here . 您可能想在这里这里阅读组合。


Update: Yep, it's that simple. 更新:是的,就是这么简单。 Your problem is exactly like counting the number of combinations of digits in a number. 您的问题就像计数一个数字中数字的组合数一样。 For example, if I want to find the number of combinations of an ATM PIN number (4 decimal digits), I have sets {0-9}, {0-9}, {0-9}, {0-9}. 例如,如果我想查找一个ATM PIN码的组合数(四位十进制数字),则可以设置{0-9},{0-9},{0-9},{0-9}。 There are 10 possibilities for the first choice (= 10). 首选有10种可能性(= 10)。 For each of those numbers, there are 10 possibilities for the second choice (= 10 × 10). 对于这些数字中的每一个,都有第二种选择的10种可能性(= 10×10)。 For each of those, there are 10 for the third (= 10 × 10 × 10) and 10 for the fourth (= 10 × 10 × 10 × 10 = 10,000). 对于每一个,第三个(= 10×10×10)有10个,第四个(= 10×10×10×10 = 10,000)有10个。 It should be intuitively clear that there are 10,000 possibilities for a 4 digit decimal number. 直观上应该清楚,四位数的十进制数有10,000种可能性。

Your example uses sets of words instead of sets of digits, but the principle is the same. 您的示例使用单词集而不是数字集,但是原理是相同的。 The number of combinations is the number of items in set 1 × number of items in set 2 × ... × number of items in set n, etc. 组合数是集合1中的项目数×集合2中的项目数×...×集合n中的项目数,依此类推。

It gets more complicated when you start putting restrictions in, or are picking multiple items from the same set, etc. 当您开始施加限制或从同一组中选择多个项目时,情况会变得更加复杂。

The problem breaks down to two simple sub-problems: 该问题分为两个简单的子问题:

  1. count how many combinations are within braces and separated within vbars, for each braces pair 计算每个括号内括号内有多少组合并在vbar内分开
  2. multiply those numbers 将这些数字相乘

So for 1 I'd go with a plain regular expression + looping approach: 所以对于1我将使用普通的正则表达式+循环方法:

import re

def docount(thestring):
    x = re.compile(r'{([^}]}')
    counts = [mo.group(0).count('|')+1 for mo in x.finditer(thestring)]
    result = 1
    for c in counts: result *= c
    return result

I've embedded 2 as well since that's the most trivial part anyway (if you're keen on using reduce for such purposes, that's OK too in lieu of the last three lines, I guess;-). 我也嵌入了2 ,因为无论如何这是最琐碎的部分(如果您热衷于出于这种目的使用reduce ,我想也可以代替最后三行;-)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM