ruby 生成长度在给定范围内的数组的所有可能组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14431582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate all possibles combinations of an array with a length within a given range
提问by fschuindt
How can I generate all possible combinations of elements of an array with a length within a given range? E.g.:
如何生成长度在给定范围内的数组元素的所有可能组合?例如:
('a'..'f').to_a.all_possibilities(3, 5)
should produce an array like:
应该产生一个数组,如:
['abc', 'abd', 'abe', 'abf', ..., 'abcde', 'abcdf', 'abcda', ...]
including from "abc"(three characters) up to the last possible combination of ('a'..'f').to_awith five characters length. I have no idea how to do this. Any help?
包括从"abc"(三个字符)到('a'..'f').to_a五个字符长度的最后一个可能的组合。我不知道该怎么做。有什么帮助吗?
回答by dbenhur
Array#combinationis stdlib:
Array#combination是标准库:
[1] pry(main)> a = ('a'..'f').to_a
=> ["a", "b", "c", "d", "e", "f"]
[2] pry(main)> a.combination(3).to_a
=> [["a", "b", "c"],
["a", "b", "d"],
["a", "b", "e"],
["a", "b", "f"],
["a", "c", "d"],
["a", "c", "e"],
["a", "c", "f"],
["a", "d", "e"],
["a", "d", "f"],
["a", "e", "f"],
["b", "c", "d"],
["b", "c", "e"],
["b", "c", "f"],
["b", "d", "e"],
["b", "d", "f"],
["b", "e", "f"],
["c", "d", "e"],
["c", "d", "f"],
["c", "e", "f"],
["d", "e", "f"]]
if you want all combinations of size min to max:
如果您想要大小最小到最大的所有组合:
(min..max).flat_map{|size| a.combination(size).to_a }
If you want them converted to strings, just replace .to_awith .map(&:join).
如果您希望它们转换为字符串,只需替换.to_a为.map(&:join).
回答by sawa
(3..5).flat_map{|n| ('a'..'f').to_a.combination(n).map(&:join)}
Edit: to meet OP's clarified intention, use repeated_permutation.
编辑:为了满足 OP 的明确意图,请使用repeated_permutation.
(3..5).flat_map{|n| ('a'..'f').to_a.repeated_permutation(n).map(&:join)}
回答by oldergod
You could modify my response to your previous question this way to get what you want.
您可以通过这种方式修改我对上一个问题的回答以获得您想要的。
class Array
def all_possibilities(from, to)
(from..to).flat_map do |i|
if i < size
permutation(i).to_a
else
permutation(to - i).flat_map do |e|
(self + e).permutation.to_a
end
end
end.map(&:join)
end
end
array = ["F", "E", "R", "N", "A", "D", "O"]
array.all_possibilities(3, 8)
回答by Emma's Fist
`
`
D = length of array
N = number of possible values. i.e a-z = 26
possible combinations = N ^ D
array = [possible values]
map26 = '0123456789abcdefghijklmnop'
map10 = 'abcdefghijklmnopqrstuvwxyz'
combo = ''
for 0...N ** D do |i|
i.to_s(D).split(//).each do |v|
combo += map10[map26.index(v)].chr
end
puts combo
combo = ''
end
`
`
EDIT: Excuse the brevity of above, hacked it out on an iPad while browsing for another answer. And I was so wrong.
编辑:请原谅上面的简洁,在浏览另一个答案时在 iPad 上将其破解。我错了。
Lets say you want all combinations from a-z for up to 3 columns.
假设您想要 az 中最多 3 列的所有组合。
All combinations are 26 * 26 * 26 = 26 ** 3 = 17576
所有组合都是 26 * 26 * 26 = 26 ** 3 = 17576
Lets subtract 1 for 0 starting points of arrays = 17575
让数组的 0 个起点减去 1 = 17575
Also we need a mapping variables, map26 is a base 26 lookup for one column
我们还需要一个映射变量,map26 是一列的基数 26 查找
map26 = '0123456789abcdefghijklmnop'
map10 = 'abcdefghijklmnopqrstuvwxyz'
Back to our maximum combo
回到我们的最大组合
17575.to_s(26)
=> "ppp"
Extract the index of 'p' from map26 and put it in map10:
从 map26 中提取 'p' 的索引并将其放入 map10:
map10[map26.index('p')] (add .chr for ruby ~ 1.8)
=> "z"
So if you slice and dice the "ppp" above you will get a maximum combo of "zzz"
因此,如果您将上面的“ppp”切片和切块,您将获得最大的“zzz”组合
You can also see from the mapping variables that "000" will map to "aaa"
还可以从映射变量中看到“000”会映射到“aaa”
I have modified the original code to include these changes.
我修改了原始代码以包含这些更改。
With respect to the original question, you can use D to control the maximum length of the string and play with the starting value of the for loop to control the minimum length.
对于原题,可以用D来控制字符串的最大长度,用for循环的起始值来控制最小长度。

