当数组数量和每个数组的长度未知时生成字符组合的所有排列
我不确定如何以简洁的方式提出我的问题,所以我将从示例开始并从示例开始扩展。我正在使用 VBA,但我认为这个问题不是特定于语言的,只需要一个可以提供伪代码框架的聪明头脑。预先感谢您的帮助!
例子: 我有 3 个字符数组,如下所示:
Arr_1 = [X,Y,Z]
Arr_2 = [A,B]
Arr_3 = [1,2,3,4]
我想生成字符数组的所有可能排列,如下所示:
XA1
XA2
XA3
XA4
XB1
XB2
XB3
XB4
YA1
YA2
.
.
.
ZB3
ZB4
这可以使用 3 个 while 循环或 for 循环轻松解决。我的问题是,如果数组的数量未知并且每个数组的长度未知,我该如何解决这个问题?
因此,作为 4 个字符数组的示例:
Arr_1 = [X,Y,Z]
Arr_2 = [A,B]
Arr_3 = [1,2,3,4]
Arr_4 = [a,b]
我需要生成:
XA1a
XA1b
XA2a
XA2b
XA3a
XA3b
XA4a
XA4b
.
.
.
ZB4a
ZB4b
所以通用示例将是:
Arr_1 = [...]
Arr_2 = [...]
Arr_3 = [...]
.
.
.
Arr_x = [...]
有没有一种方法可以构造一个函数,该函数将生成未知数量的循环并循环遍历每个数组的长度以生成排列?或者也许有更好的方法来思考这个问题?
谢谢大家!
I'm not sure how to ask my question in a succinct way, so I'll start with examples and expand from there. I am working with VBA, but I think this problem is non language specific and would only require a bright mind that can provide a pseudo code framework. Thanks in advance for the help!
Example:
I have 3 Character Arrays Like So:
Arr_1 = [X,Y,Z]
Arr_2 = [A,B]
Arr_3 = [1,2,3,4]
I would like to generate ALL possible permutations of the character arrays like so:
XA1
XA2
XA3
XA4
XB1
XB2
XB3
XB4
YA1
YA2
.
.
.
ZB3
ZB4
This can be easily solved using 3 while loops or for loops. My question is how do I solve for this if the # of arrays is unknown and the length of each array is unknown?
So as an example with 4 character arrays:
Arr_1 = [X,Y,Z]
Arr_2 = [A,B]
Arr_3 = [1,2,3,4]
Arr_4 = [a,b]
I would need to generate:
XA1a
XA1b
XA2a
XA2b
XA3a
XA3b
XA4a
XA4b
.
.
.
ZB4a
ZB4b
So the Generalized Example would be:
Arr_1 = [...]
Arr_2 = [...]
Arr_3 = [...]
.
.
.
Arr_x = [...]
Is there a way to structure a function that will generate an unknown number of loops and loop through the length of each array to generate the permutations? Or maybe there's a better way to think about the problem?
Thanks Everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
递归解决方案
这实际上是最简单、最直接的解决方案。以下是 Java 语言,但应该具有指导意义:(
查看完整输出)
注意:Java 数组是从 0 开始的,因此
k
在递归过程中从0..arrs.length-1
开始,直到k == arrs.length
结束递归。非递归解决方案
也可以编写非递归解决方案,但坦率地说,这不太直观。这实际上与基数转换非常相似,例如从十进制到十六进制;这是一种通用形式,其中每个位置都有自己的一组值。
(查看完整输出)
这会以不同的顺序生成连音。如果您想以与递归解决方案相同的顺序生成它们,则可以在
decode
期间“向后”迭代arrs
,如下所示:(查看完整输出)
Recursive solution
This is actually the easiest, most straightforward solution. The following is in Java, but it should be instructive:
(see full output)
Note: Java arrays are 0-based, so
k
goes from0..arrs.length-1
during the recursion, untilk == arrs.length
when it's the end of recursion.Non-recursive solution
It's also possible to write a non-recursive solution, but frankly this is less intuitive. This is actually very similar to base conversion, e.g. from decimal to hexadecimal; it's a generalized form where each position have their own set of values.
(see full output)
This produces the tuplets in a different order. If you want to generate them in the same order as the recursive solution, then you iterate through
arrs
"backward" duringdecode
as follows:(see full output)
感谢@polygenelubricants 提供了出色的解决方案。
这是 Javascript 的等价内容:
Thanks to @polygenelubricants for the excellent solution.
Here is the Javascript equivalent:
编辑:这是一个红宝石解决方案。它与我下面的其他解决方案几乎相同,但假设您的输入字符数组是单词:因此您可以输入:
~/bin/perm.rb
OLD - 我有在 MEL(Maya 的嵌入式语言)中执行此操作的脚本 - 我将尝试翻译为类似 C 的语言,但不要指望它无需进行任何修复即可运行;)但它可以在 Maya 中运行。
首先 - 将所有数组放在一个带有分隔符的长数组中。 (我将把它留给你 - 因为在我的系统中它会从 UI 中删除值)。因此,这意味着分隔符将占用额外的插槽: 要使用上面的示例数据:
当然,您可以根据需要连接任意数量的数组。
EDIT: Here's a ruby solution. Its pretty much the same as my other solution below, but assumes your input character arrays are words: So you can type:
~/bin/perm.rb
OLD - I have a script to do this in MEL, (Maya's Embedded Language) - I'll try to translate to something C like, but don't expect it to run without a bit of fixing;) It works in Maya though.
First - throw all the arrays together in one long array with delimiters. (I'll leave that to you - because in my system it rips the values out of a UI). So, this means the delimiters will be taking up extra slots: To use your sample data above:
Of course you can concatenate as many arrays as you like.
如果我正确理解了这个问题,我认为您可以将所有数组放入另一个数组中,从而创建一个锯齿状数组。
然后,循环遍历锯齿状数组中的所有数组,创建所需的所有排列。
这有道理吗?
If I understand the question correctly, I think you could put all your arrays into another array, thereby creating a jagged array.
Then, loop through all the arrays in your jagged array creating all the permutations you need.
Does that make sense?
听起来你已经差不多明白了。
如果您再放入一个数组,称之为 ArrayHolder ,它包含所有未知数量、未知长度的数组,该怎么办?然后,你只需要另一个循环,不是吗?
it sounds like you've almost got it figured out already.
What if you put in there one more array, call it, say
ArrayHolder
, that holds all of your unknown number of arrays of unknown length. Then, you just need another loop, no?