Created
December 2, 2011 16:00
-
-
Save masylum/1423750 to your computer and use it in GitHub Desktop.
The tuesday birthday problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
print ''' | |
You meet a man on the street and he says, "I have two children and one is a son born on a Tuesday." | |
What is the probability that the other child is also a son? | |
''' | |
def get_combinations(): | |
children = ('s', 'd') | |
weekdays = tuple(range(1, 8)) | |
known_kid = ('s', 2) | |
def valid_combination(x, y, z, w): | |
return (x, z) == known_kid or (y, w) == known_kid | |
return [(x, y) for x in children for y in children for z in weekdays for w in weekdays if valid_combination(x, y, z, w)] | |
def test(num_tests): | |
"Benchmark to convince people that the answer is not 1/2" | |
combinations = get_combinations() | |
def increase_if_two_sons(accumulator, _): | |
if random.choice(combinations) == ('s', 's'): accumulator += 1 | |
return accumulator | |
num_two_sons = reduce(increase_if_two_sons, [None] * num_tests, 0) | |
print "Probability: %.2f" % ((float(num_two_sons) / num_tests) * 100) | |
print "Estimated Probability: %.2f" % (13.0 / 27 * 100) | |
test(1000000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment