0

私は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],
  ]

うまくいけば、ここで何が起こっているのかが明確になります。2ではなく3の引数で呼び出した場合、9つの要素の配列を取得し、そのうちの1つは%w[eggs, eggs, eggs, bacon, sausage]です。

私はこれを書く方法を思い付くのに苦労しています。助言がありますか?

4

2 に答える 2

1

@sawaが指摘しているように、おそらく結果がまちまちです。2を渡すと6ではなく8の要素が得られ、3を渡すと9ではなく27の要素が得られると思います。

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これはRuby1.9.2の新機能であることに注意してください。require 'backports'以前のバージョンで使用することも、次を使用して醜いバージョンを使用することもできますproduct

# ...
(1..n).to_a.product(*[(1..n).to_a] * (size-1)).map do |repeats|
# ...
于 2012-11-27T03:43:43.657 に答える
0

この組み込みの順列メソッドを試すことができます。

   %w[eggs bacon sausage].permutation(2).to_a
于 2012-11-27T03:35:09.697 に答える