Skip to content

Instantly share code, notes, and snippets.

@priyankvex
Created December 29, 2018 10:39
Show Gist options
  • Save priyankvex/da9788106e2f7836add5ef1f9d663c40 to your computer and use it in GitHub Desktop.
Save priyankvex/da9788106e2f7836add5ef1f9d663c40 to your computer and use it in GitHub Desktop.
Scamming the coding interview : Problem 013 : Unbiased coin toss from a biased coin.
"""
Scamming the coding interview. Problem 013
Subscribe to our newsletter at https://scammingthecodinginterview.com/
to get a coding or design problem daily in your inbox and become exceptionally good at
coding interviews.
"""
from random import randint
def biased_coin_toss():
n = randint(0, 5)
if n%2 == 0:
# probability (2/5)
return "H"
else:
# probability (3/5)
return "T"
def toss_coin():
toss_1 = biased_coin_toss()
toss_2 = biased_coin_toss()
if toss_1 == toss_2:
return toss_coin()
if toss_1 == "T" and toss_2 == "H":
return "T"
elif toss_1 == "H" and toss_2 == "T":
return "H"
if __name__ == '__main__':
print(toss_coin())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment