News for hackers. Stuff that matters.
Re:Coding Horror: Finishing The Game by ynd on Friday January 02, 2009 @03:13AM (#417198)
# 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
Re:Coding Horror: Finishing The Game by ynd on Sunday January 04, 2009 @09:29PM (#419893)
# 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))