Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

# 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))


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: