如何从可变数量的可变长度数组中找到由 1 个元素组成的所有排列?
我有一个由长度不同的数组 D
组成的数组 U
。我需要能够返回数组索引的所有排列,这些排列将选择由每个集合中的 1 个元素组成的不同排列。我还要求将该算法表示为仅记住最后一个排列的对象,并使用 get_next 方法返回下一个排列。
例如,U = [array_of_size_n1, array_of_size_n2, array_of_size_n3]
将有 n1*n2*n3
个排列,每个排列 3 个元素长。
编辑:组数也有所不同。
I have an array U
of arrays D
that vary in length. I need to be able to return all permutations of array indices that would select a different permutation consisting of 1 element from each set. I also require that this alorithm gets represented as an object that only remembers the last permutation, and returns the next permutation with a get_next method.
For instance, U = [array_of_size_n1, array_of_size_n2, array_of_size_n3]
There would be n1*n2*n3
permutations, each 3 elements long.
Edit: the number of sets also varies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 python,这是标准库的一部分:
itertools.product
。但假设您不是,这里有一个伪代码版本。您可以像这样使用它(假设您的数组都不为空)。此示例打印出数组中元素的每个组合。
If you're using python, this is part of the standard library:
itertools.product
. But assuming you're not, here's a pseudocode version.You can use it like this (assuming none of your arrays are empty). This example prints out every combination of elements from the arrays.
为了继续理解 Anon 所说的内容,你不能只是循环播放它们。您在类中维护状态,以便知道每个数组的最后一个索引是什么。逻辑是相同的,但是您不会在连续循环中运行。伪代码逻辑将是:
To tack on to what Anon said, you don't just loop over them. You maintain state in your class so that you know what your last index was for each array. The logic is the same, but you don't run in a continuous loop. The pseudo-code logic would be:
那么...这个不简单吗?
你想要一个迭代器。您希望它迭代最后一个数组。当到达该数组的末尾时,增加其在倒数第二个数组中的当前位置,然后返回到最后一个数组的开头。
使用 C#s
yield return
语法的伪代码:编辑:如果集合的数量变化,您可以使用某种形式的递归:
考虑传入长度为 1 的列表时的行为,然后考虑该行为长度为 2 的列表,并让自己确信它确实有效。
So ... what about this isn't straightforward?
You want an iterator. You want it to iterate over the last array. When it gets to the end of that array, increment its current position in the second-last array and go back to the start of the last array.
psuedocode using C#s
yield return
syntax:EDIT: If the number of sets varies, you could use some form of recursion:
Consider the behaviour when a list of length 1 is passed in, then consider the behaviour for a list of length 2, and satisfy yourself that it does in fact work.
您可以只为每个数组中您的个人位置保留一个计数器。在 get_next 方法中,将计数器增加 1 并按数组长度对其进行 mod 。然后,每当前一个计数器变为 0 时,您只需增加下一个计数器即可;
编辑:在数组数量变化的情况下,将位置变量保存在数组中。事实上,无论如何,这可能会更好。这样您就可以像引用数组本身一样引用 pos 变量。
You could just keep a counter for your individual position in each array. In your get_next method increase the counter for one and mod it by the length of the array. Then you just increase the next counter every time the previous one rolls over to 0;
EDIT: In the case the number of arrays varies, hold your position variables in an array. Actually that would probably be better anyway. That way you can refer to the pos variable in the same way you refer to the array itself.