红心国王
King of Hearts
题目详情
你拿一副标准的洗牌牌,不断翻转牌,直到出现第一张 A,即牌#14。下一张牌是红心K的概率是多少
You take a standard shuffled deck of cards and keep flipping cards over until the first ace appears, which is card #14. What's the probability the next card is the king of hearts
解析
我们知道第 14 张牌一定是 A,而它之前的 13 张牌不可能是 A。因此,我们将 48 张非 A 牌中的 13 张排列为前 13 张,A 排列为第 14 位,并对其余 38 张牌进行排列以计算可能选择的总数。 为了求出第 15 张牌是 8h 的概率,我们需要求出 Kh 位于第 15 位且为有效选择的选择数。我们将 47 张非 A 非 Kh 牌中的 13 张排列为前 13 个位置,将 A 排列为第 14 个位置,将 8h 排列为第 15 个位置,然后排列其余 37 张牌以获得: 将其与我们得到的可能选择总数相加:
import random
suits = ['S', 'C', 'D', 'H']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
class Deck:
def __init__(self):
self.cards = []
for suit in suits:
for rank in ranks:
self.cards.append([rank, suit])
def shuffle(self):
# shuffles deck of cards
return random.sample(self.cards, 52)
# number of decks where the first ace is on the 14th draw
num_valid_decks = 0
num_15th_is8h = 0
# create 10,000,000 shuffled decks
# see how many of the decks, where the first ace is on the 14th draw, have 8h as the next card
for i in range(1_000_000):
deck = Deck().shuffle()
# excluding all decks with an ace not being on the 14th draw
if deck[13][0] != 'A':
continue
# exlcuding all decks with an ace in the first 13 draws
for j in range(13):
if deck[j][0] == 'A':
break
else:
num_valid_decks += 1
#checking if 15th card is 8 of hearts
if deck[14]== ['K', 'H']:
num_15th_is8h += 1
# should be expecting 35/1824 ~ 0.0191886
print(num_15th_is8h / num_valid_decks)Original Explanation
We know the 14th card must be an ace and the 13 cards that precede it can not be an ace. Thus we permute 13 of the 48 non-ace cards to be among the first 13, an ace to be in spot 14, and permute the remaining 38 cards to count the total number of possible selections. To find the probability the 15th card is 8h we need to find the number of selections with Kh being in spot 15 while being a valid selection. We permute 13 of the 47 non-ace non-Kh cards for the first 13 spots, an ace for spot 14, the 8h for spot 15, and permute the remaining 37 cards to get: Putting this over the total number of possible selections we get:
import random
suits = ['S', 'C', 'D', 'H']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
class Deck:
def __init__(self):
self.cards = []
for suit in suits:
for rank in ranks:
self.cards.append([rank, suit])
def shuffle(self):
# shuffles deck of cards
return random.sample(self.cards, 52)
# number of decks where the first ace is on the 14th draw
num_valid_decks = 0
num_15th_is8h = 0
# create 10,000,000 shuffled decks
# see how many of the decks, where the first ace is on the 14th draw, have 8h as the next card
for i in range(1_000_000):
deck = Deck().shuffle()
# excluding all decks with an ace not being on the 14th draw
if deck[13][0] != 'A':
continue
# exlcuding all decks with an ace in the first 13 draws
for j in range(13):
if deck[j][0] == 'A':
break
else:
num_valid_decks += 1
#checking if 15th card is 8 of hearts
if deck[14]== ['K', 'H']:
num_15th_is8h += 1
# should be expecting 35/1824 ~ 0.0191886
print(num_15th_is8h / num_valid_decks)