Created
December 7, 2018 21:59
-
-
Save machinaut/93f1b86f48fdd8e2560a2c17c0aa7254 to your computer and use it in GitHub Desktop.
Umbrellas Riddler
This file contains 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
#!/usr/bin/env python | |
from itertools import product | |
PROB_MORNING_RAIN = .5 | |
PROB_EVENING_RAIN = .4 | |
# Enumerate all possible cases, and sum the probability of getting rained on | |
total_probability = 0 # Total probability of getting rained on | |
for rains in product(*[[True, False]]*10): | |
# Calculate the probability of outcome and whether Louie was rained on | |
rained_on = False | |
probability = 1.0 | |
home, office = 2, 1 # Number of umbrellas at each location | |
for i in range(5): | |
# Morning | |
if rains[2 * i]: | |
probability *= PROB_MORNING_RAIN | |
if home > 0: | |
home -= 1 | |
office += 1 | |
else: | |
rained_on = True | |
else: | |
probability *= 1 - PROB_MORNING_RAIN | |
# Evening | |
if rains[2 * i + 1]: | |
probability *= PROB_EVENING_RAIN | |
if office > 0: | |
office -= 1 | |
home += 1 | |
else: | |
rained_on = True | |
else: | |
probability *= 1 - PROB_EVENING_RAIN | |
# Add resulting probability if Louis got rained on | |
total_probability += probability if rained_on else 0 | |
print(total_probability) # 0.3073 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment