Created
March 2, 2014 23:10
-
-
Save skyl/9315377 to your computer and use it in GitHub Desktop.
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
""" | |
https://brilliant.org/community-problem/loser-keeps-bieber-we-stood-no-chance/ | |
?group=4cgSapeNTVXO&ref_id=115277 | |
""" | |
import random | |
def play_defensive(them): | |
if random.random() <= 0.04: | |
them += 1 | |
return them | |
def play_offensive(us, them): | |
r1 = random.random() | |
r2 = random.random() | |
if r1 <= 0.05: | |
them += 1 | |
if r2 >= 0.95: | |
us += 1 | |
return us, them | |
def simulate(us=1, them=0, defensive=20, minutes_left=20): | |
""" | |
If leading with less than `defensive` minutes left, | |
play_defensive, else play_offensive. | |
""" | |
for minute in range(minutes_left, 0, -1): | |
if defensive >= minute and (us > them): | |
them = play_defensive(them) | |
else: | |
us, them = play_offensive(us, them) | |
if us > them: | |
return 1.0 | |
if them > us: | |
return 0.0 | |
return 0.5 | |
A = 0 | |
B = 0 | |
C = 0 | |
D = 0 | |
tries = 100000 | |
for i in range(tries): | |
A += simulate(defensive=10) | |
B += simulate(defensive=20) | |
C += simulate(defensive=15) | |
D += simulate(defensive=5) | |
print A | |
print B | |
print C | |
print D | |
import IPython; IPython.embed() |
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
$ python optimal_strategy.py | |
76397.5 | |
72070.0 | |
74839.5 | |
76561.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment