Mercurial > hg > python
view nhist.py @ 29:7d6bc790b213
simulation of multi-segment coin flip with bias
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Wed, 28 Jul 2021 17:55:36 +0100 |
parents | bd1db1ed4c25 |
children |
line wrap: on
line source
#!/usr/bin/python # histogram counts of numeric input, uses existing counts if given # Usage: nhist.py [-c] [-p] [binwidth] [pointCol countCol] # Default binwidth is 100 import sys bins={} minv=sys.maxint maxv=-sys.maxint-1 cum=False percent=False while len(sys.argv)>1: if sys.argv[1]=='-c': sys.argv.pop(1) cum=True elif sys.argv[1]=='-p': sys.argv.pop(1) cum=True percent=True tot=0 else: break if len(sys.argv)>1: w=int(sys.argv[1]) else: w=100 if len(sys.argv)>2: pc=int(sys.argv[2]) cc=int(sys.argv[3]) counts=True else: counts=False for l in sys.stdin: if counts: ff=l.split() n=int(ff[pc]) c=int(ff[cc]) else: n=int(l) c=1 v=n/w if percent: tot+=c bins[v]=bins.get(v,0)+c if n<minv: minv=min if n>maxv: maxv=max if cum: cumTot=0 for k in sorted(bins.keys()): if cum: cumTot+=bins[k] print k,bins[k], if cum: print cumTot, if percent: print "%5.2f"%(float(cumTot)*100/tot) else: print