view nhist.py @ 55:68004ce55703

convert to newer source of Nonogram data
author Henry Thompson <ht@markup.co.uk>
date Mon, 29 May 2023 22:24:53 +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