4 ms·
# The hard part: THIS IS AN 'A POSTERIORI' PROBLEM. # The fact as ALREADY happened. # BG/GB being DIFFERENT is NOT THE TWIST. # Whether they are or not it wi
by ynd 18y ago
# The hard part: THIS IS AN 'A POSTERIORI' PROBLEM.
# The fact as ALREADY happened.
# BG/GB being DIFFERENT is NOT THE TWIST.
# Whether they are or not it will amount to 50% total.
# TWIST: BB is not possible.
def boy_girl_problem():
from random import choice
families = {}
for i in range(1000):
families[i] = []
families[i].append(choice(['BOY', 'GIRL']))
families[i].append(choice(['BOY', 'GIRL']))
had_both_sex = filter(lambda f: set(f) == set(['BOY', 'GIRL']), families.values())
had_two_boys = filter(lambda f: 'GIRL' not in f, families.values())
return len(had_both_sex) / float(len(families) - len(had_two_boys))
# average(boy_girl_problem) == 2/3
- ynd 18y ago# Here's a less crude rewrite # It reflects conditional probability better def boy_girl_problem(): from random import choice families = {} for i in range(1000): families[i] = [] families[i].append(choice(['BOY', 'GIRL'])) families[i].append(choice(['BOY', 'GIRL'])) had_both_sex = filter(lambda f: set(f) == set(['BOY', 'GIRL']), families.values()) had_one_girl = filter(lambda f: 'GIRL' in f, families.values()) return len(had_both_sex) / float(len(had_one_girl))