2016-10-09 6 views
-2

私はその中の配列を持つ2つの異なるアレイを持っている:シャッフル二つの配列非ランダム

Vowels = [['a', 'a'], ['a', 'e'], ['a', 'i']] 
Consonants = [['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'b', 'd']] 

私は機能

で、コンテンツを失ったり、追加することなく、すべての組み合わせの中にそれらを一緒に混合することができるようにしたいです
allmixes(Vowels, Consonants) 
#=> [['a', 'a', 'b', 'b', 'b'], ['a', 'b', 'b', 'b', 'a'], ...] 

私が2つの配列に2つの配列を必要とするのは、重複がないか、インデックスに基づいて特定の結果を呼び出すパターンに従うシャッフルです。

答えて

0

あなたがpermutationで試すことができます。

vowels.flat_map do |vowel| 
    consonants.flat_map do |consonant| 
    [*vowel,*consonant].permutation.to_a.uniq 
    end 
end 

​​

+0

私は '母音を設定した場合=' Vowels'、私の中で定義された 'Consonants'ためVowels'と'子音= Consonants'を、答え、あなたのコードは、私のコードで取得する同じ配列(120要素)を生成します。 –

+0

@CarySwoveland:申し訳ありませんが、あなたのポイントは何ですか(睡眠障害)? – potashin

+1

ちょうど私たちはどちらかが正しいか両方が間違っています。 –

1
Vowels = [['a', 'a'], ['a', 'e']] 
Consonants = [['b', 'b', 'b'], ['b', 'b', 'c']] 

Vowels.product(Consonants).flat_map { |v,c| (v+c).permutation.to_a.uniq } 
    #=> [["a", "a", "b", "b", "b"], ["a", "b", "a", "b", "b"], ["a", "b", "b", "a", "b"], 
     ["a", "b", "b", "b", "a"], ["b", "a", "a", "b", "b"], ["b", "a", "b", "a", "b"], 
     ["b", "a", "b", "b", "a"], ["b", "b", "a", "a", "b"], ["b", "b", "a", "b", "a"], 
     ["b", "b", "b", "a", "a"], ["a", "a", "b", "b", "c"], ["a", "a", "b", "c", "b"], 
     ["a", "a", "c", "b", "b"], ["a", "b", "a", "b", "c"], ["a", "b", "a", "c", "b"], 
     ... 
     ["c", "b", "a", "e", "b"], ["c", "b", "a", "b", "e"], ["c", "b", "e", "a", "b"], 
     ["c", "b", "e", "b", "a"], ["c", "b", "b", "a", "e"], ["c", "b", "b", "e", "a"]] 

Vowels.product(Consonants).flat_map { |v,c| (v+c).permutation.to_a.uniq }.size 
    #=> 120 
関連する問題