Skip to content

Instantly share code, notes, and snippets.

@allencch
Last active March 14, 2018 10:19
Show Gist options
  • Save allencch/1b06e97ca3f96ac4bf795e1fdad08b17 to your computer and use it in GitHub Desktop.
Save allencch/1b06e97ca3f96ac4bf795e1fdad08b17 to your computer and use it in GitHub Desktop.
Frog Riddle
#!/usr/bin/env python
import random
# Frog 0 for female, 1 for male
def create_frog():
return random.randint(0, 1)
def has_croak(pairs): # also male
return 1 in pairs
def has_female(frogs):
return 0 in frogs
def choose_without_croak(choose_two):
frogs = [create_frog() for i in range(3)]
# first frog at the right side
# second and third at the left side
if choose_two:
return has_female(frogs[1:]) # choose two frogs
return has_female(frogs[0:1])
def main():
total = 10000
correct = 0
for i in range(total):
correct += choose_without_croak(True)
print('Just choose two frogs. Total: {}, correct: {}. P = {}'.format(total, correct, correct / total))
correct = 0
for i in range(total):
correct += choose_without_croak(False)
print('Just choose one frog. Total: {}, correct: {}. P = {}'.format(total, correct, correct / total))
# The exact question is,
# "What is the probability of the frogs in the pair has female,
# given that one of them is male?"
def exact_calculation():
total = 10000
croak = 0
correct = 0
for i in range(total):
frogs = [create_frog() for i in range(3)]
if has_croak(frogs[1:]):
croak += 1
if has_female(frogs[1:]):
correct += 1
print('Total croak: {}, correct: {}. P = {}'.format(croak, correct, correct / croak))
main()
exact_calculation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment