简体   繁体   English

Ruby所有可能的数组排列(一个内衬?)

[英]Ruby all possible permutations of an array of arrays (one liner?)

Questions similar to this have been asked before on SO, but they're not quite what I need and I can't seem to arrive at my solution through altering/modifying those approaches. 之前在SO上已经提出了与此类似的问题,但它们并不是我所需要的,我似乎无法通过改变/修改这些方法来达到我的解决方案。

In any case, I have an array of arrays, as follows: 无论如何,我有一个数组数组,如下所示:

b= [["1"],["2"],["3"],["4"],["5"],["6"]]

(If it makes it easier to arrive at a solution, b can also be a one dimensional array, as follows: ["1","2","3","4","5","6"] . Either type of input works for my needs.) (如果它更容易得到解决方案, b也可以是一维数组,如下所示: ["1","2","3","4","5","6"] 。任何类型的输入都可以满足我的需求。)

and I would like to generate the following: 我想生成以下内容:

[["123456"],["213456"],["312456"],...] 

where each array in the output array is a unique permutation of the six numbers. 其中输出数组中的每个数组都是六个数字的唯一排列。 I would also take it as a single array (eg, ["123456", "213456",...] ). 我也将它作为单个数组(例如, ["123456", "213456",...] )。 The order of the output isn't particularly important as long as each entry is unique and no number repeats in a string (eg, "112345" isn't allowed). 只要每个条目是唯一的并且在字符串中没有数字重复(例如,不允许“112345”),输出的顺序就不是特别重要。 All 6 numbers must also be used in each entry, so I'm not interested in incremental output like "123" , either. 所有6个数字也必须在每个条目中使用,所以我对增量输出也不感兴趣,如"123"

As much as this sounds like it, this isn't a homework problem. 尽管听起来像这样,但这不是一个家庭作业问题。 I could brute for this thing and get the output I need. 我可以为这件事做事,并得到我需要的输出。 I just feel like there has to be a better, more elegant, solution. 我觉得必须有一个更好,更优雅的解决方案。

With Array#permutation : 使用Array#permutation

permutations = (1..6).to_a.permutation.map(&:join)
# ["123456", "123465", "123546", ..., "654312", "654321"]

Ruby does this natively :) From the ruby documentation : Ruby本地做到这一点:)来自ruby文档:

a = [1, 2, 3]
a.permutation.to_a     #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutation(1).to_a  #=> [[1],[2],[3]]
a.permutation(2).to_a  #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
a.permutation(3).to_a  #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutation(0).to_a  #=> [[]] # one permutation of length 0
a.permutation(4).to_a  #=> []   # no permutations of length 4

http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-permutation http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-permutation

You should definitely have a look at Permutation Gem . 你一定要看看Permutation Gem Example from documentation 文档示例

perm = Permutation.new(3)
# => #<Permutation:0x57dc94 @last=5, @rank=0, @size=3>
colors = [:r, :g, :b]
# => [:r, :g, :b]
perm.map { |p| p.project(colors) }
# => [[:r, :g, :b], [:r, :b, :g], [:g, :r, :b], [:g, :b, :r], [:b, :r, :g],
#  [:b, :g, :r]]

UPDATE UPDATE

If you are using Ruby > 1.8.6, Array.permutation is built in. 如果您使用的是Ruby> 1.8.6,则内置Array.permutation

这应该这样做:

b.permutation.to_a.collect! { |i| i = [i.flatten.join] }

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

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