我正在尝试编写一个Array
扩展名来执行以下操作:
我称之为:
%w[eggs bacon sausage].with_every_possibiity_of_multiples(2)
它给了我这些:
[
%w[eggs, bacon, sausage],
%w[eggs, eggs, bacon, sausage],
%w[eggs, bacon, bacon, sausage],
%w[eggs, bacon, sausage, sausage],
%w[eggs, eggs, bacon, bacon, sausage],
%w[eggs, eggs, bacon, bacon, sausage, sausage],
]
希望很清楚这里发生了什么。如果我用3而不是2的参数调用它,我会得到一个包含9个元素的数组,其中一个是%w[eggs, eggs, eggs, bacon, sausage]
。
我正在努力想出如何写这篇文章。有什么建议吗?
答案 0 :(得分:1)
正如@sawa指出的那样,你可能会把你的结果搞得一团糟。我猜测传球2可以获得8个元素,而不是6个,传球3可以获得27个而不是9个。
您可以充分利用Array#repeated_combination
来生成重复每个元素的次数:
class Array
def with_every_possibility_of_multiples(n)
(1..n).to_a.repeated_permutation(size).map do |repeats|
zip(repeats).flat_map{|elem, nb| [elem] * nb}
end
end
end
food = %w[eggs bacon sausage]
food.with_every_possibility_of_multiples(2) # =>
[["eggs", "bacon", "sausage"], ["eggs", "bacon", "sausage", "sausage"], ["eggs", "bacon", "bacon", "sausage"],
["eggs", "bacon", "bacon", "sausage", "sausage"],
["eggs", "eggs", "bacon", "sausage"],
["eggs", "eggs", "bacon", "sausage", "sausage"],
["eggs", "eggs", "bacon", "bacon", "sausage"],
["eggs", "eggs", "bacon", "bacon", "sausage", "sausage"]]
food.with_every_possibility_of_multiples(3) # =>
[["eggs", "bacon", "sausage"],
["eggs", "bacon", "sausage", "sausage"],
["eggs", "bacon", "sausage", "sausage", "sausage"],
["eggs", "bacon", "bacon", "sausage"],
["eggs", "bacon", "bacon", "sausage", "sausage"],
["eggs", "bacon", "bacon", "sausage", "sausage", "sausage"],
["eggs", "bacon", "bacon", "bacon", "sausage"],
["eggs", "bacon", "bacon", "bacon", "sausage", "sausage"],
["eggs", "bacon", "bacon", "bacon", "sausage", "sausage", "sausage"],
["eggs", "eggs", "bacon", "sausage"],
["eggs", "eggs", "bacon", "sausage", "sausage"],
["eggs", "eggs", "bacon", "sausage", "sausage", "sausage"],
["eggs", "eggs", "bacon", "bacon", "sausage"],
["eggs", "eggs", "bacon", "bacon", "sausage", "sausage"],
["eggs", "eggs", "bacon", "bacon", "sausage", "sausage", "sausage"],
["eggs", "eggs", "bacon", "bacon", "bacon", "sausage"],
["eggs", "eggs", "bacon", "bacon", "bacon", "sausage", "sausage"],
["eggs", "eggs", "bacon", "bacon", "bacon", "sausage", "sausage", "sausage"],
["eggs", "eggs", "eggs", "bacon", "sausage"],
["eggs", "eggs", "eggs", "bacon", "sausage", "sausage"],
["eggs", "eggs", "eggs", "bacon", "sausage", "sausage", "sausage"],
["eggs", "eggs", "eggs", "bacon", "bacon", "sausage"],
["eggs", "eggs", "eggs", "bacon", "bacon", "sausage", "sausage"],
["eggs", "eggs", "eggs", "bacon", "bacon", "sausage", "sausage", "sausage"],
["eggs", "eggs", "eggs", "bacon", "bacon", "bacon", "sausage"],
["eggs", "eggs", "eggs", "bacon", "bacon", "bacon", "sausage", "sausage"],
["eggs", "eggs", "eggs", "bacon", "bacon", "bacon", "sausage", "sausage", "sausage"]]
请注意repeated_permutation
是Ruby 1.9.2的新功能。您可以在早期版本中使用require 'backports'
,或使用product
使用uglier版本:
# ...
(1..n).to_a.product(*[(1..n).to_a] * (size-1)).map do |repeats|
# ...
答案 1 :(得分:0)
您可以尝试这种内置排列方法:
%w[eggs bacon sausage].permutation(2).to_a