Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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?

share|improve this question
1  
What is the relation between your input and output? –  M42 Feb 19 '13 at 10:51
3  
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
1  
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
show 3 more comments

4 Answers

up vote 6 down vote accepted

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

*
*
* **
****
share|improve this answer
1  
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
1  
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
1  
@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
add comment

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
*   
*   
* * 
****
share|improve this answer
    
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
add comment

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.

share|improve this answer
    
Err... umm... thanks ? :) –  Majid Feb 19 '13 at 14:56
add comment

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
*       
*       
*   *   
* * * * 
share|improve this answer
1  
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
    
I accepted this one as it is in a language I might possibly use. Apologies to the other great entries. –  Majid Feb 25 '13 at 20:43
    
I unaccepted as I see I wasn't following the house rules. Sorry. –  Majid Feb 27 '13 at 21:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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