Histogram generation
Python - 83 characters
Seems that we can take input from anywhere, so this takes input during execution, rather than from the command line, and uses Ejrb's suggestion to shorten it by 8.
s=map(len,raw_input().split())
c=0;exec'c+=1;print"%3d|"%c+"#"*s.count(c);'*max(s)
Python - 91 characters
This will fall over with quotes.
import sys;s=map(len,sys.argv[1:])
for i in range(1,max(s)+1):print"%3d|"%i+'#'*s.count(i)
Input:
> python hist.py Please write a specification rather than giving a single example which, solely by virtue of being a single example, cannot express the range of acceptable output styles, and which doesnt guarantee to cover all corner cases. Its good to have a few test cases, but its even more important to have a good spec.
Output:
1|#####
2|######
3|#####
4|##########
5|######
6|#############
7|####
8|#
9|##
10|#
11|
12|
13|#
R, 55 47 characters
hist(a<-sapply(scan(,""),nchar),br=.5+0:max(a))
Luckily R comes with a plot function hist
for histograms, here supplied with a breaks
argument where the breaks are 0.5, 1.5, ... until max(input)+0.5. sapply(scan(,""),nchar)
takes an input (as stdin), separates it following the spaces and count the number of characters of each element.
Examples:
hist(a<-sapply(scan(,""),nchar),br=.5+0:max(a))
1: Extensive word length should not be very problematic.
9:
Read 8 items
hist(a<-sapply(scan(,""),nchar),br=.5+0:max(a))
1: Very long strings of words should be just as easy to generate a histogram just as short strings of words are easy to generate a histogram for.
28:
Read 27 items
Edit:
A variation at 71 characters with an axis label at each possible value:hist(a<-sapply(scan(,""),nchar),br=.5+0:max(a),ax=F);axis(1,at=1:max(a))
Haskell - 126 characters
p[d]=[' ',d];p n=n
h l=[1..maximum l]>>= \i->p(show i)++'|':(l>>=($"#").drop.abs.(i-))++"\n"
main=interact$h.map length.words
This takes the input from stdin
, not the command line:
& head -500 /usr/share/dict/words | runhaskell 15791-Histogram.hs
1|##
2|##
3|######
4|###############
5|################################################
6|###############################################################
7|###################################################################
8|###########################################################################
9|#############################################################
10|##########################################################
11|#########################################################
12|#########################
13|#######
14|###
15|#####
16|###
17|#
18|
19|#
20|#