r/dailyprogrammer • • Jul 14 '12
[7/13/2012] Challenge #76 [intermediate] (Probability graph)
Write a function graph(f, low, high, tests) that outputs a probability graph of the function f from range low to high (inclusive) over tests tests (i.e., counting the frequencies of f() outputs). f takes no arguments and returns an integer, low, high and tests are all integer values. For example, a function f that simulates two-dice rolls:
def two_dice():
return random.randint(1, 6) + random.randint(1, 6)Then graph(f, 2, 12, 10000) should output something roughly like:
2: ##
3: #####
4: #######
5: ###########
6: #############
7: #################
8: #############
9: ###########
10: ########
11: #####
12: ##For bonus points, output the graph with the numbers on the bottom and the bars drawn vertically.
7
Upvotes
3
u/benzinonapoloni Jul 14 '12
Python
2
u/skeeto -9 8 Jul 14 '12 edited Jul 14 '12
And the output:
This: This is fun to play with.
Output:
2
u/semicolondash Jul 14 '12
C++. Not well formatted result but it gets the job done. I'll work on the bonus of formatting it nicely.
1
u/flowblok Jul 15 '12
A while ago, I wrote a script called "chart", which I’ve put below. It’s really useful, and means that my solution to this problem is a unix pipeline. Anyhow, put this script on your $PATH as "chart":
And then you can run this pipeline:
Which produces:
Actually, the "sort -n | uniq -c" could be replaced with a single command which used less memory, since sort needs to retain all the output, whereas that part of the pipeline just needs to put things into buckets and count them.
1
u/AlexDiru Jul 15 '12
C# - extra optional parameter to scale the graph
1
u/ae7c Jul 18 '12 edited Jul 18 '12
Python
1
u/Fontong Jul 14 '12 edited Jul 14 '12
Python:
Horizontal graph solution:
And the output:
Vertical graph solution:
And the output:
1
u/devilsassassin Jul 15 '12
In Java with the horizontal output:
Output: