Skip to content

Instantly share code, notes, and snippets.

@OzuYatamutsu
Created September 4, 2015 06:08
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 OzuYatamutsu/f46d38a0e739e59ae343 to your computer and use it in GitHub Desktop.
Save OzuYatamutsu/f46d38a0e739e59ae343 to your computer and use it in GitHub Desktop.
# The Tuesday Child Puzzle!
# I have two children.
# One of them is a girl born on a Tuesday.
# What is the probability that I have two girls?
from enum import Enum
from random import randint
class Child():
def __init__(self, sex, day):
self.sex = sex
self.day = day
class Days(Enum):
MONDAY = 0
TUESDAY = 1
WEDNESDAY = 2
THURSDAY = 3
FRIDAY = 4
SATURDAY = 5
SUNDAY = 6
class Sex(Enum):
BOY = 0
GIRL = 1
def birth_child():
birth_sex = Sex(randint(Sex.BOY.value, Sex.GIRL.value))
birth_day = Days(randint(Days.MONDAY.value, Days.SUNDAY.value))
print("Birthing a " + str(birth_sex) + " on a " + str(birth_day))
return Child(birth_sex, birth_day)
def has_tuesday_girl(trial):
for child in trial:
if child.sex is Sex.GIRL and child.day is Days.TUESDAY:
return True
return False
def both_are_girls(trial):
return trial[0].sex is Sex.GIRL and trial[1].sex is Sex.GIRL
def do_trial():
return birth_child(), birth_child()
def main():
max_trials = 100000
total_trials = 0
trials = [do_trial() for trial in range(0, max_trials)]
has_tuesday_girls = [trial for trial in trials if has_tuesday_girl(trial)]
result = [trial for trial in has_tuesday_girls if both_are_girls(trial)]
per_born_on_tuesday = round(100 * len(has_tuesday_girls) / max_trials, 2)
per_result = round(100 * len(result) / len(has_tuesday_girls), 2)
per_error = round(100 * (abs(per_result - (100 * (13/27))) / (100 * (13/27))), 2)
print("\nOut of " + str(max_trials) + " trials:")
print(str(len(has_tuesday_girls)) + " have girls born on a Tuesday (" + str(per_born_on_tuesday) + "%)")
print(str(len(result)) + " of those have two girls (" + str(per_result) + "%)")
print("Expected probability: 13/27 (" + str(per_error) + "% error)")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment