Skip to content

Instantly share code, notes, and snippets.

Created July 3, 2017 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7bfa5c7036bdee3fa7e45bd33268d1aa to your computer and use it in GitHub Desktop.
Save anonymous/7bfa5c7036bdee3fa7e45bd33268d1aa to your computer and use it in GitHub Desktop.
import random
def generate_choices():
yield 4
yield 5
yield 6
yield 7
while True:
yield 8
def get_tries():
# 26 cards from A to Z
cards = list(range(26))
# one card is removed at random
removed_card = random.choice(cards)
del cards[removed_card]
# remaining cards shuffled (not actually necessary)
random.shuffle(cards)
# list of all letters from A to Z
letter_list = list(range(26))
# initially N is 4
tries = 1
for N in generate_choices():
# choose N letters from list
N_letters = random.sample(letter_list, min(N, len(letter_list)))
for letter in N_letters:
if letter == removed_card:
return tries
# cross out
del letter_list[letter_list.index(letter)]
# draw two letters from shuffled deck
two_letters = random.sample(cards, min(2, len(cards)))
for letter in two_letters:
if letter == removed_card:
return tries
if letter in letter_list:
# cross out
del letter_list[letter_list.index(letter)]
# discard
del cards[cards.index(letter)]
tries += 1
def generate_expected_value():
total_count = 0
tries_count = 0.0
while True:
total_count += 1
tries_count += get_tries()
if total_count % 1000 == 0:
expected_tries = tries_count / total_count
yield expected_tries
def generate_probabilities():
values = {}
trials = 0
while True:
tries = get_tries()
trials += 1
if tries not in values:
values[tries] = 0
values[tries] += 1
if trials % 1000 == 0:
yield sorted([(i[0], 100*round(i[1]/float(trials),2)) for i in values.items()])
if __name__ == "__main__":
for p in generate_probabilities():
print(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment