Skip to content

Instantly share code, notes, and snippets.

@rsha256
Created November 3, 2020 01:31
Show Gist options
  • Save rsha256/5f7965f327459910458207bcad1d0d14 to your computer and use it in GitHub Desktop.
Save rsha256/5f7965f327459910458207bcad1d0d14 to your computer and use it in GitHub Desktop.
simple coin toss visualizer for a fair coin's sample space
def product(*iters):
def loop(prod, first=[], *rest):
if not rest:
for x in first:
yield prod + (x, )
else:
for x in first:
yield from loop(prod + (x, ), *rest)
yield from loop((), *iters)
def toss_coins_h(n):
sides = ['H', 'T']
coins = [sides] * n
yield from product(*coins)
def toss_coins(n):
for i in toss_coins_h(n):
print(i)
toss_coins(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment