5
\$\begingroup\$

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?

\$\endgroup\$
9
  • 1
    \$\begingroup\$ What is the relation between your input and output? \$\endgroup\$
    – Toto
    Feb 19, 2013 at 10:51
  • 3
    \$\begingroup\$ 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). \$\endgroup\$
    – plannapus
    Feb 19, 2013 at 10:56
  • \$\begingroup\$ @plannapus. That's right. Thanks. \$\endgroup\$
    – Majid
    Feb 19, 2013 at 10:58
  • \$\begingroup\$ Related to an oldie on Stack Overflow: Code golf: Word frequency chart. \$\endgroup\$ Feb 19, 2013 at 19:48
  • 1
    \$\begingroup\$ Are the bin sizes integers also. If so what answer do you get for: 4 1 1 2 2 5 8 9 6 3 \$\endgroup\$
    – Graham
    Feb 20, 2013 at 10:55

4 Answers 4

6
\$\begingroup\$

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

*
*
* **
****
\$\endgroup\$
4
  • 1
    \$\begingroup\$ How can you run it at tryapl.org? It gives me invalid token error in the ways I tried. \$\endgroup\$
    – randomra
    Feb 19, 2013 at 15:32
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Graham
    Feb 19, 2013 at 15:59
  • 1
    \$\begingroup\$ @Graham How do you even type that line on a normal keyboard? \$\endgroup\$
    – Majid
    Feb 19, 2013 at 16:11
  • \$\begingroup\$ 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. \$\endgroup\$
    – Graham
    Feb 19, 2013 at 16:32
3
\$\begingroup\$

GolfScript, 57 55 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
*   
*   
* * 
****
\$\endgroup\$
1
  • \$\begingroup\$ 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? \$\endgroup\$
    – Graham
    Feb 21, 2013 at 10:24
2
\$\begingroup\$

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.

\$\endgroup\$
0
1
\$\begingroup\$

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: 
Read 8 items
*   
* * 
* * 
* * 

> source('asciihist.R')
1: 4 1 1 2 2 5 8 6 3
10: 
Read 9 items
*       
*       
*   *   
* * * * 
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Due to vector recycling, you could replace rep("\n",d) with "\n". This would save 7 characters. \$\endgroup\$ Feb 19, 2013 at 16:05
  • \$\begingroup\$ @SvenHohenstein Better than that actually: I don't need to declare d anymore! Thanks! \$\endgroup\$
    – plannapus
    Feb 19, 2013 at 16:25
  • \$\begingroup\$ I accepted this one as it is in a language I might possibly use. Apologies to the other great entries. \$\endgroup\$
    – Majid
    Feb 25, 2013 at 20:43
  • \$\begingroup\$ I unaccepted as I see I wasn't following the house rules. Sorry. \$\endgroup\$
    – Majid
    Feb 27, 2013 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.