Skip to content

Instantly share code, notes, and snippets.

@vr2262
Last active December 12, 2015 09:08
Show Gist options
  • Save vr2262/4748707 to your computer and use it in GitHub Desktop.
Save vr2262/4748707 to your computer and use it in GitHub Desktop.
import sys
import argparse
import itertools as it
import numpy as np
def flipper(n):
def cool_range_doritos(*args, **kwargs):
if sys.version_info[0] < 3:
return xrange(*args, **kwargs)
else:
return range(*args, **kwargs)
flips = [np.random.randint(2) for _ in cool_range_doritos(n)]
chain_lengths = list(len(list(chain)) for _, chain in it.groupby(flips))
result = enumerate(np.bincount(chain_lengths))
next(result)
return result
def int_greater_than_zero(arg):
msg = "'{}' is not an integer greater than zero.".format(arg)
try:
value = int(arg)
except ValueError:
raise argparse.ArgumentTypeError(msg)
if not value > 0:
raise argparse.ArgumentTypeError(msg)
return value
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="""
Displays the lengths of chains of the same
result after a certain number of coin
flips.""")
parser.add_argument("num_flips", help="The total number of flips.",
type=int_greater_than_zero)
args = parser.parse_args()
for pair in flipper(args.num_flips):
print(pair)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment