-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhist
More file actions
executable file
·56 lines (43 loc) · 1.22 KB
/
hist
File metadata and controls
executable file
·56 lines (43 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
# print an ascii histogram of the frequencies of integers presented at stdin
# TODO: make it work for floats
import sys
from subprocess import check_output
def getScreenDimensions():
return (int(check_output(['tput','lines']))-3, int(check_output(['tput','cols'])))
if __name__=='__main__':
# load data
data = {}
for line in sys.stdin.readlines():
try:
i = int(line)
except ValueError:
continue
if i in data:
data[i]+=1
else:
data[i]=1
dataMin = min(data)
dataMax = max(data)
(rows, cols) = getScreenDimensions()
bins = [0] * cols
binSize = (dataMax - dataMin - 1) // cols + 1
maxCount = 0
for key in data:
count = data[key]
if count > maxCount:
maxCount = count
bins[(key-dataMin) // binSize] += count
heightScale = rows / maxCount
heights = [int(count*heightScale) for count in bins]
print(heights)
# print a graph
for row in range(rows-1,-1,-1):
height = row# * heightScale
S = ""
for col in range(cols):
if heights[col] >= height:
S += "*"
else:
S += " "
print(S)