Skip to content

Instantly share code, notes, and snippets.

@EvanKrall
Created April 22, 2011 05:33
Show Gist options
  • Save EvanKrall/936101 to your computer and use it in GitHub Desktop.
Save EvanKrall/936101 to your computer and use it in GitHub Desktop.
Given a list of numbers separated by newlines, prints out a histogram.
#!/bin/bash
NUMTESTS=0
histogram=()
while read num
do
((NUMTESTS++))
((histogram[num]++))
done
maxIndex=0
maxCount=0
for index in ${!histogram[*]}
do
if ((index>maxIndex))
then
maxIndex=$index
fi
if ((histogram[index]>maxCount))
then
maxCount=${histogram[index]}
fi
done
for ((i=maxCount; i>0; i--))
do
printf '%3d' $i
for ((j=0; j<=maxIndex; j++))
do
if ((histogram[j] >= i))
then
printf ' # '
else
printf ' '
fi
done
echo
done
echo -n ' '
for ((j=0; j<=maxIndex; j++))
do
printf '%2d ' $j
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment