rubyで組み合わせを列挙したりするメゾッド
combination
(1..4).to_a.combination(2).to_a
=> [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
permutation
(1..4).to_a.permutation(2).to_a
=> [[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4, 2], [4, 3]]
- combinationから
[2, 1], [3, 2]
等が増えた
- combinationから
repeated_combination
(1..4).to_a.repeated_combination(2).to_a
=> [[1, 1], [1, 2], [1, 3], [1, 4], [2, 2], [2, 3], [2, 4], [3, 3], [3, 4], [4, 4]]
- combinationから
[1, 1], [2, 2]
等が増えた
- combinationから
repeated_permutation
(1..4).to_a.repeated_permutation(2).to_a
=> [[1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 2], [2, 3], [2, 4], [3, 1], [3, 2], [3, 3], [3, 4], [4, 1], [4, 2], [4, 3], [4, 4]]
- permutationから
[1, 1], [2, 2]
等が増えた
- permutationから
combination は順序で結果が変わらない奴に
permutation は変わる奴に
repeated_ がつくと要素の重複を許す