Paste #1971

Welcome On LodgeIt

Welcome to the Enzotools pastebin, an installation of LodgeIt. In order to use the notification feature a 31 day cookie with an unique ID was created for you. The lodgeit database does not store any information about you, it's just used for an advanced pastebin experience :-). Read more on the about lodgeit page. Have fun :-)

hide this notification

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
### This is a brute force way of answering the following problem:
###         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?

import random
import sys

def get_child():
    # sex is a random integer either 0 or 1.
    sex = random.randint(0, 1)
    # birthday is a random integer from 0 through 6.
    birthday = random.randint(0, 6)
    return (sex, birthday)

def get_child_pair():
    child1 = get_child()
    child2 = get_child()
    return (child1, child2)

def filter_children(sex=0, birthday=2):
    child1, child2 = get_child_pair()
    # If either child has the sex and birthday we want, 
    # return the other child.
    # If neither child has the requirements, return None.
    if child1[0] == sex and child1[1] == birthday:
        return child2
    if child2[0] == sex and child2[1] == birthday:
        return child1
    return None

# Run until we have this many in our sample.
max_matches = 1000000
# Count the number of boys and girls returned.
boys = 0
girls = 0

# Count the total of attempts, just for fun.
tries = 0

# The sex we're looking for.
# You should still get the same answer, whether 
# this is 0 or 1.
target = 0

n_matches = 0
while n_matches < max_matches:
    tries += 1
    p_child = filter_children(sex=target)
    # If the filter returns none, just skip and move on.
    if p_child is None: continue
    # If the filter returned, then check the sex of the other child.
    if p_child[0] == target:
        boys += 1
    else:
        girls += 1

    n_matches += 1
    sys.stderr.write("Computing: %d/%d\r" % (n_matches, max_matches))
sys.stderr.write("\n")

print "Total matches: %d, boys: %d [%.2f]%%, girls: %d [%.2f]%%, total tries: %d." % \
    (n_matches,
     boys, (100 * float(boys) / n_matches), 
     girls, (100 * float(girls) / n_matches),
     tries)