# The tuesday boy problem
# http://s...content-available-to-author-only...s.com/evolutionblog/2011/11/08/the-tuesday-birthday-problem/
# http://e...content-available-to-author-only...a.org/wiki/Boy_or_Girl_paradox
# Sounds complicated and counter-intuitive, right?
# Python to the rescue!
# I have changed the wording in the problem to be less ambiguous:
# "If you ask someone whom you know has two children, whether they have
# at least one boy born on a tuesday, what is the probability that
# both children are boys?"
import itertools
import fractions
GENDERS = 'boy girl'.split()
DAYS = 'mon tue wed thu fri sat sun'.split()
def has_two_boys((child1, child2)):
#Do you have two boys?
return child1[0] == 'boy' and child2[0] == 'boy'
def question(child):
#Is this child a boy born on a tuesday?
return child == ('boy', 'tue')
def show_probability(pairs):
nr_boy_pairs = len(filter(has_two_boys, pairs))
print '%d pairs, where %d are boy pairs' % (len(pairs), nr_boy_pairs)
print 'gives', fractions.Fraction(nr_boy_pairs, len(pairs))
print
CHILD_TYPES = list(itertools.product(GENDERS, DAYS))
#All these pairs are equally common
#(if we pretend that each day and gender is equally likely for each birth):
ALL_CHILD_PAIRS = list(itertools.product(CHILD_TYPES, CHILD_TYPES))
print 'Probability that a person has two boys:'
show_probability(ALL_CHILD_PAIRS)
#Now we ask all parents the question
YES_PARENTS = [pair for pair in ALL_CHILD_PAIRS
#Is any of your children a boy born on a tuesday?
if question(pair[0]) or question(pair[1])]
print 'Probability that a person has two boys, given that they answer yes:'
show_probability(YES_PARENTS)