Last active
March 3, 2016 16:53
-
-
Save jessebot/0acd2892a6c27ed3c885 to your computer and use it in GitHub Desktop.
A fun interviewing problem!
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/python | |
# assumes python 2.7 | |
# Script by [email protected] | |
# 3/2/16 | |
import random | |
""" | |
Assume there is a room full of coins and a robot that can interact with them. | |
The robot picks up each coin and if the coin was heads face up, it flips it to | |
tails. If the coin is tails up, the robot tosses the coin into the air. We | |
assume the robot is picking up coins at random. Wite a program to give the | |
percentage of heads and tails coins when the robot is done. Allow number of | |
coins to be given, and also allow iterations to be set, (some tests should | |
include what would happen if the interactions were an order of magnitude or | |
two larger than amount of coins). Print percentages before and after. | |
""" | |
def calculate_start(coins, interactions, num_of_heads_start): | |
"""Just make sure typing is correct for all these integers and if not, | |
just make up a coin number for them. Let's do this.""" | |
# verify we got integers and if not, just keep on truckin' | |
try: | |
int(coins) | |
except ValueError: | |
# this is just for the proof of concept, I could handle this gracefully | |
coins = 500 | |
finally: | |
if not interactions: | |
interactions = coins * (10**2) | |
# more verification | |
try: | |
int(num_of_heads_start) | |
except ValueError: | |
# the show must go on... | |
num_of_heads_start = coins / 2 | |
# get starting tails | |
num_of_tails_start = coins - num_of_heads_start | |
# give starting percentages | |
tails_percent = 100 * float(num_of_tails_start)/float(coins) | |
heads_percent = 100 * float(num_of_heads_start)/float(coins) | |
# let user know what's up | |
print "Starting Tails: {0} which is {1} percent".format(num_of_tails_start, | |
tails_percent) | |
print "Starting Heads: {0} which is {1} percent".format(num_of_heads_start, | |
heads_percent) | |
return coins, num_of_tails_start, num_of_heads_start, interactions | |
def calculate_coin_faces(coins=500, interactions=50000, | |
num_of_heads_start=250): | |
"""Takes three integers: coins, interactions, num_of_heads_start | |
and will calculate the resulting percentage of heads and tails facing | |
coins. Assume we always flip heads to tails and tails into the air. | |
Prints percentage before and after. If nothing given, uses 500 coins, | |
50000 interactions, and 250 number of heads to start values.""" | |
start = calculate_start(coins, interactions, num_of_heads_start) | |
coins = start[0] | |
tails = start[1] | |
heads = start[2] | |
interactions = start[3] | |
# assume beginning tails | |
final_numbers = {'final_heads': heads, 'final_tails': tails} | |
# start range calc for interactions and coins (python range is silly) | |
total_interactions = interactions + 1 | |
coins_range = coins + 1 | |
# begin looping to play with your coins | |
for interaction in range(1, total_interactions): | |
# grab a random coin off the floor | |
rando_coin = random.randint(1, coins_range) | |
# if heads | |
if rando_coin in range(1, final_numbers['final_heads']): | |
final_numbers['final_heads'] -= 1 | |
final_numbers['final_tails'] += 1 | |
# if tails | |
else: | |
flip = random.randint(0, 1) | |
if flip == 0: | |
final_numbers['final_heads'] += 1 | |
final_numbers['final_tails'] -= 1 | |
# calculate percentages | |
heads = final_numbers['final_heads'] | |
tails = final_numbers['final_tails'] | |
tails_percent = 100 * float(tails)/float(coins) | |
heads_percent = 100 * float(heads)/float(coins) | |
# tell the user what's up | |
print "Ending Tails: {0} which is {1} percent".format(tails, | |
tails_percent) | |
print "Ending Heads: {0} which is {1} percent".format(heads, | |
heads_percent) | |
return heads_percent, tails_percent | |
if __name__ == '__main__': | |
while True: | |
interactive = raw_input("Would you like an interactive demonstration?" + | |
" (y/n) ") | |
if interactive == 'n': | |
print "Okie dokie, on with the show, we'll assume 500 coins," + \ | |
" 50000 interactions, and 250 heads to start." | |
calculate_coin_faces() | |
break | |
elif interactive == 'y': | |
coins = int(raw_input("How many coins? ")) | |
interactions = int(raw_input("How many interactions should we do? ")) | |
num_of_heads_start = int(raw_input("How many coins should be heads " + | |
"to start? ")) | |
calculate_coin_faces(coins, interactions, num_of_heads_start) | |
break | |
else: | |
print "Please give y or n as an answer." | |
print "Thanks for playing!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment