リストのy要素から選択されたx個別のオブジェクトのすべての組み合わせを印刷します
-
29-09-2019 - |
質問
たとえば、52枚のカードのポーカーハンドの5枚のカード= 2598960の組み合わせ。
しかし、実際にこれらすべての組み合わせを表示するにはどうすればよいですか?
番号を見つけるためのコードは簡単です:
def binomial_coef(total,subset)
factorial(total) / (factorial(subset) * factorial(total - subset))
end
def factorial(n)
n.downto(1).inject(:*)
end
# different 5 card poker hand combinations
cards = 52
hand_number = 5
puts binomial_coef(cards, hand_number)
すべての実際の組み合わせを印刷するためのソリューションに関するアイデアはありますか?
例えば:
1,2,3,4,5
1,2,3,4,6
等
または、Gettingsが始まるのを助けることさえできます。ありがとう!
解決
あなたが必要です Array#combination
cards = (1..52).to_a
hand_number = 5
cards.combination(hand_number).to_a
=> [[1,2,3,4,5],[1,2,3,4,6],...]
他のヒント
(1..52).to_a.combination(5)
puts (1..52).to_a.combination(5).to_a.inspect
所属していません StackOverflow