Created
September 2, 2017 19:45
-
-
Save rvcx/0c0967934c7b2944979fd0c98e938354 to your computer and use it in GitHub Desktop.
five-minute solution for express puzzle from https://fivethirtyeight.com/features/is-your-friend-full-of-it/
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 python3 | |
import argparse | |
import sys | |
import random | |
def simulate(accuracy, goal): | |
i, streak = 0, 0 | |
while streak < goal: | |
i += 1 | |
if random.random() < accuracy: | |
streak += 1 | |
else: | |
streak = 0 | |
return i | |
def avg(vals): | |
total, num = 0, 0 | |
for v in vals: | |
total += v | |
num += 1 | |
return total / float(num) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--trials', type=int, default=10000) | |
parser.add_argument('--goal', type=int, default=17) | |
parser.add_argument('--accuracy', type=float, default=0.7) | |
args = parser.parse_args() | |
print(avg(simulate(args.accuracy, args.goal) for i in range(args.trials))) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment