要生產一個交易,你會使用random.sample()
function隨機挑選6張牌;給第3〜播放器1,其餘部分與玩家2:
dealt_cards = random.sample(deck_of_cards, 6)
player1_hand, player2_hand = dealt_cards[:3], dealt_cards[3:]
爲了產生所有可能的組合然後,你會使用itertools.combinations()
與大小6;但你仍然需要找到將這些6張卡片2手特有的所有可能的排列:
for dealt_cards in itertools.combinations(deck_of_cards, 6):
for hand1 in itertools.combinations(dealt_cards, 3):
hand2 = tuple(card for card in dealt_cards if card not in hand1)
從6張牌每個處理組合
左右,3張牌所有組合被賦予一個球員,離開其他玩家剩下3張牌。
由此產生的解決方案的預期數量:
>>> from itertools import combinations
>>> deck = range(9)
>>> sum(1 for dealt in combinations(deck, 6) for _ in combinations(dealt, 3))
1680
和演示:
>>> for dealt_cards in combinations(deck, 6):
... for hand1 in combinations(dealt_cards, 3):
... hand2 = tuple(card for card in dealt_cards if card not in hand1)
... print hand1, hand2
...
(0, 1, 2) (3, 4, 5)
(0, 1, 3) (2, 4, 5)
(0, 1, 4) (2, 3, 5)
(0, 1, 5) (2, 3, 4)
(0, 2, 3) (1, 4, 5)
# ...
(4, 6, 7) (3, 5, 8)
(4, 6, 8) (3, 5, 7)
(4, 7, 8) (3, 5, 6)
(5, 6, 7) (3, 4, 8)
(5, 6, 8) (3, 4, 7)
(5, 7, 8) (3, 4, 6)
(6, 7, 8) (3, 4, 5)
你也可以製作只播放1手,然後與甲板的剩餘生產繼續玩家2手:
for hand1 in itertools.combinations(deck_of_cards, 3):
for hand2 in itertools.combinations([c for c in deck_of_cards if c not in hand1], 3):
# ...
具有相同的預期計數輸出和結果:
>>> sum(1 for dealt in combinations(deck, 3) for _ in combinations([c for c in deck if c not in dealt], 3))
1680
>>> for hand1 in combinations(deck, 3):
... for hand2 in combinations([c for c in deck if c not in hand1], 3):
... print hand1, hand2
...
(0, 1, 2) (3, 4, 5)
(0, 1, 2) (3, 4, 6)
(0, 1, 2) (3, 4, 7)
(0, 1, 2) (3, 4, 8)
(0, 1, 2) (3, 5, 6)
# ...
(6, 7, 8) (1, 3, 4)
(6, 7, 8) (1, 3, 5)
(6, 7, 8) (1, 4, 5)
(6, 7, 8) (2, 3, 4)
(6, 7, 8) (2, 3, 5)
(6, 7, 8) (2, 4, 5)
(6, 7, 8) (3, 4, 5)
儘管現在順序不同。
您的n_o_c公式缺少'/(N-K)'因素! – DSM
@DSM和結果巧合正確:) – bereal
@DSM OMG。謝謝 –