# Print ASCII histogram

The challenge is to give as short code as possible to print an ASCII histogram of a set of data.

Input. The number of bins followed by a number of integers representing the data. The data are space separated.

Output. An ASCII histogram of the data using the specified number of bins.

Score. Number of characters in the code excluding trailing whitespace at the beginning or end of each line.

Example input.

2    2    3    4    2    2    5


Example output.

*
*
**
**


Update. The answers are all great although I can only ever imagine using the one in R. No python contribution?

• What is the relation between your input and output? – Toto Feb 19 '13 at 10:51
• I think the first value of the input is the number of bins and all the others the vector to proceed: if you split it into two bins of equal size, the first one contains 4 values (three 2s and one 3) and the second 2 values (a 4 and a 5). – plannapus Feb 19 '13 at 10:56
• @plannapus. That's right. Thanks. – Majid Feb 19 '13 at 10:58
• Related to an oldie on Stack Overflow: Code golf: Word frequency chart. – dmckee Feb 19 '13 at 19:48
• Are the bin sizes integers also. If so what answer do you get for: 4 1 1 2 2 5 8 9 6 3 – Graham Feb 20 '13 at 10:55

## APL 66 51

' *'[1+(⌽⍳b)∘.≤v←+/(⍳b)∘.=⌈x[⍋x]÷(⌈/x←1↓v)÷b←↑v←⍎⍞]


Using the example I quoted in my comment on the question where the bin size is 2.25:

4 1 1 2 2 5 8 9 6 3

*
*
* **
****

• How can you run it at tryapl.org? It gives me invalid token error in the ways I tried. – randomra Feb 19 '13 at 15:32
• I can very rarely get that to work. If you want to play around with APL then I suggest you download the free interpreter from here: nars2000.org – Graham Feb 19 '13 at 15:59
• @Graham How do you even type that line on a normal keyboard? – Majid Feb 19 '13 at 16:11
• You don't without an APL interpreter. If you download the interpreter I suggested above you can then either use a keyboard map to type the APL, use the on screen point and click APL keyboard or simply copy and paste from this page. – Graham Feb 19 '13 at 16:32

### GolfScript, 5755 52 characters

~](\$:?;:b,{{\?0=-b*?)\0=-)/=" *"=}+?%$}%zip{32-},n*


Taking input as single line from STDIN. Script can be tested online.

Example:

> 4 1 1 2 2 5 8 6 3
*
*
* *
****

• Following on from my comment on the question what bin size does your code give for an input of: 4 1 1 2 2 5 8 9 6 3 and are you happy with the ASCII result? – Graham Feb 21 '13 at 10:24

## J, 96 86

(' *'{~|.@|:@(>/i.@(>./))@(}:@}:,{:@}:+{:)@(+/@(=/i.@>:@{.)~<.@({.*(%>./)@(-<./)@}.)))


In a little more readable format: http://pastebin.com/2CiaJXjC.

   (...) 2 2 3 4 2 2 5
*
*
**
**
(...) 4 1 1 2 2 5 8 6 3
*
*
* *
****


Step by step on example values: http://pastebin.com/nXjGcq8L.

• Err... umm... thanks ? :) – Majid Feb 19 '13 at 14:56

## R - 133 122 characters

a=scan()
b=table(cut(a[-1],a[1]))
invisible(apply(cbind(sapply(b,function(x)c(rep(" ",max(b)-x),rep("*",x))),"\n"),1,cat))


When sourced, it prompts the user for input (because of scan), and output the ascii histogram.

> source('asciihist.R')
1: 2 2 3 4 5 2 2 5
9:
*
* *
* *
* *

> source('asciihist.R')
1: 4 1 1 2 2 5 8 6 3
10:

• Due to vector recycling, you could replace rep("\n",d) with "\n". This would save 7 characters. – Sven Hohenstein Feb 19 '13 at 16:05
• @SvenHohenstein Better than that actually: I don't need to declare d anymore! Thanks! – plannapus Feb 19 '13 at 16:25