确定可能组合的数量

发布于 2024-08-16 21:09:36 字数 462 浏览 2 评论 0原文

我试图找出有多少种可能的方法来组合该字符串中的各种元素。

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

其中从每个组 ({}) 中随机选择一项(由竖线/| 分隔)并组合成单个字符串。

所以上面的“模板”可以产生:

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

我猜这是一种排列,但我想确保我得到了正确的结果。

如果这也适用于“n”个嵌套项目,那就太好了。

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

如果可能的话,我更喜欢基于数学/统计的解决方案而不是暴力循环来获得答案。

谢谢!

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.

"{{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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨敛清荷 2024-08-23 21:09:36

嗯,第一个示例中有 3 x 2 x 3 = 18 种组合。

第二个示例是 3 x 4 x 2 x 3 = 72 种组合。

我不完全确定 {a|b}|{c|d} 你的意思,我假设你的意思是选择(a或b)或(c或d)之一),有4个选择。

您可能想在此处此处


更新:是的,就是这么简单。你的问题就像计算一个数字中数字组合的数量一样。例如,如果我想查找 ATM PIN 码(4 位十进制数字)的组合数,我有集合 {0-9}、{0-9}、{0-9}、{0-9}。第一选择有 10 种可能性 (= 10)。对于每个数字,第二个选择有 10 种可能性 (= 10 × 10)。对于其中每一个,第三个有 10 个 (= 10 × 10 × 10),第四个有 10 个 (= 10 × 10 × 10 × 10 = 10,000)。直观上应该很清楚,4 位十进制数有 10,000 种可能性。

您的示例使用单词集而不是数字集,但原理是相同的。组合数是集合 1 中的项目数 × 集合 2 中的项目数 × ... × 集合 n 中的项目数等。

当您开始添加限制或从中选择多个项目时,情况会变得更加复杂同一套等

Well, there are 3 x 2 x 3 = 18 combinations in your first example.

Your second example is 3 x 4 x 2 x 3 = 72 combinations.

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.

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}. There are 10 possibilities for the first choice (= 10). For each of those numbers, there are 10 possibilities for the second choice (= 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). It should be intuitively clear that there are 10,000 possibilities for a 4 digit decimal number.

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.

It gets more complicated when you start putting restrictions in, or are picking multiple items from the same set, etc.

淡墨 2024-08-23 21:09:36

这个问题分解为两个简单的子问题:

  1. 计算大括号内有多少个组合并在 vbar 内分隔,对于每个大括号
  2. 对乘以这些数字

所以对于 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

我也嵌入了 2 ,因为无论如何这是最微不足道的部分(如果你热衷于使用 reduce 来实现这样的目的,那也可以代替我猜最后三行;-)。

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
  2. multiply those numbers

So for 1 I'd go with a plain regular expression + looping approach:

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;-).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文