葫蘆(Full house)
回到我們的撲克牌遊戲。上次你計算了拿到至少一組「同點數的兩張牌」的機率。這次我們要關注的是「葫蘆」。葫蘆是指你拿到兩張具有相同點數、但花色不同的牌,以及另外三張也具有相同點數的牌(例如:紅心 2 和黑桃 2,加上梅花 J、方塊 J、黑桃 J)。
因此,葫蘆可以理解為:在已經拿到「另一個點數的兩張牌」的條件下,恰好再拿到「同點數三張牌」的條件機率。請沿用之前的程式碼,修改成功條件以得到想要的輸出。透過這個練習,你會學到如何在牌類遊戲中估計條件機率,並為以模擬方式建構抽象問題打下基礎。
本練習屬於課程
Python 的統計模擬
練習說明
- 先將
deck_of_cards洗牌。 - 使用帶有
.get()的字典來計算手牌中每個點數出現的次數。 - 當手牌形成葫蘆時(某一點數有 2 張,另一點數有 3 張),將計數器
full_house加一。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Shuffle deck & count card occurrences in the hand
n_sims, full_house, deck_of_cards = 50000, 0, deck.copy()
for i in range(n_sims):
____
hand, cards_in_hand = deck_of_cards[0:5], {}
for card in hand:
# Use .get() method to count occurrences of each card
cards_in_hand[card[1]] = cards_in_hand.____(card[1], 0) + 1
# Condition for getting full house
condition = (max(cards_in_hand.values()) ==3) & (min(cards_in_hand.values())==2)
if condition:
full_house ____
print("Probability of seeing a full house = {}".format(full_house/n_sims))