Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 26
Image: ubuntu2004
Kernel: Python 3 (system-wide)

Suppose you draw cards from a standard 52-card deck until you obtain a pair (two cards having the same value). What is the expected number of cards you have to draw in order for this to happen?

import numpy as np values = ['2','3','4','5','6','7','8','9','10','J','K','Q','A'] suits = ['H','D','S','C'] cards = [[v,s] for v in values for s in suits]
def sim_one_pair(): np.random.shuffle(cards) value_list = [] count_taken = 0 pair = 0 for card in cards: count_taken += 1 #Count each time w value = card[0] if not value in value_list: value_list.append(value) if len(value_list) == 4: #If value list have exactly 4, it means that there is a pair. pair += 1 if pair == 1: #When we get a pair, display how many times it took. return count_taken print(value_list) sim_one_pair()
4