Last active
March 20, 2020 12:07
-
-
Save Thyrst/662db2741e8e456114174f354997a6e8 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
#!/usr/bin/env python | |
# | |
# Based on https://www.youtube.com/watch?v=cpwSGsb-rTs | |
# | |
# changed to refute counterclaim by MindYourDecisions: | |
# - https://www.youtube.com/watch?v=go3xtDdsNQM | |
# | |
# more discussion about the riddle here: | |
# - https://www.reddit.com/r/math/comments/48vzz8/why_tededs_frog_riddle_is_wrong/ | |
# | |
import random | |
import copy | |
import collections | |
Frog = collections.namedtuple('Frog', 'sex croaked') | |
MALE = False | |
FEMALE = True | |
class FrogGroup(list): | |
def include(self, sex): | |
return any(frog.sex is sex for frog in self) | |
@property | |
def croaked(self): | |
return sum(frog.croaked for frog in self) | |
choices = (MALE, FEMALE) | |
def spawn_frog(): | |
sex = random.choice(choices) | |
if sex is MALE: | |
# we don't know what is probability that a male will croak | |
# for simplicity let's assume that | |
# there is a 50% probability that a male will croak in our decision making timespan | |
croaked = random.choice((True, False)) | |
else: | |
# if female croaks or not is irrelevant | |
# we count in just MALE CROAKING which is DISTINCTIVE | |
croaked = False | |
return Frog(sex, croaked) | |
def get_croaking_idx(frogs): | |
for i, frog in enumerate(frogs): | |
if frog.croaked: | |
return i | |
def get_survived_percentage(survived_data): | |
return (100 * sum(survived_data)/len(survived_data)) | |
survived_on_left = [] | |
survived_on_right = [] | |
i = 0 | |
while i < 1000000: | |
frogs_on_left = FrogGroup([spawn_frog(), spawn_frog()]) | |
frogs_on_right = FrogGroup([spawn_frog()]) | |
if frogs_on_left.croaked == 1 and frogs_on_right.croaked == 0: | |
# you can decrease number of iterations and print idx | |
# to "see" the croaking frog | |
idx = get_croaking_idx(frogs_on_left) | |
# our scenario | |
i += 1 | |
survived_on_left.append(frogs_on_left.include(FEMALE)) | |
survived_on_right.append(frogs_on_right.include(FEMALE)) | |
print("Survive ratio on left: %.0f%%" % get_survived_percentage(survived_on_left)) | |
print("Survive ratio on right: %.0f%%" % get_survived_percentage(survived_on_right)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment