forked from ewilderj/coin-tosser
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
*Simple coin tosser* | ||
|
||
Simulate the results of a coin toss experiment. | ||
|
||
eg. (chart-scores (sample-scores 200 20)) will yield: | ||
|
||
-13 | ||
-12 * | ||
-11 | ||
-10 ******* | ||
-9 | ||
-8 ******* | ||
-7 | ||
-6 ***************** | ||
-5 | ||
-4 ******************* | ||
-3 | ||
-2 ************************* | ||
-1 | ||
0 ********************************* | ||
1 | ||
2 ****************************** | ||
3 | ||
4 ***************************** | ||
5 | ||
6 ******************* | ||
7 | ||
8 *********** | ||
9 | ||
10 ** | ||
11 | ||
12 | ||
13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(defproject coins "1.0.0" | ||
:description "coin tossing" | ||
:dev-dependencies []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
(use '[clojure.string :only (join)]) | ||
|
||
(defn coin-toss | ||
"Tosses a fair coin and returns true for heads, false for tails" | ||
[] | ||
(= 1 (rand-int 2))) | ||
|
||
(defn toss-score | ||
"Win $1 for heads, lose $1 for tails" | ||
[toss] | ||
(if toss 1 -1)) | ||
|
||
(defn count-winnings | ||
"Figure out the net result for a sequence of tosses" | ||
[results] | ||
(reduce + (map toss-score results))) | ||
|
||
(defn sample-score | ||
"Run an experiment of n tosses and figure out the result" | ||
[n] | ||
(count-winnings (repeatedly n coin-toss))) | ||
|
||
(defn sample-scores | ||
"Run a sequence of experiments n times with m tosses" | ||
[n m] | ||
(repeatedly n #(sample-score m))) | ||
|
||
(defn tally-scores | ||
"Return a map of frequency of results" | ||
[scores] | ||
(let | ||
;; define an auxiliary function that registers a | ||
;; score into a map holding all the score tallies | ||
[tally-score (fn [results score] | ||
(if-let [prev-count (results score)] | ||
(assoc results score (inc prev-count)) | ||
(assoc results score 1)))] | ||
(reduce tally-score {} scores))) | ||
|
||
(defn chart-scores | ||
"Given a list of scores, print a simple ASCII frequency graph" | ||
[scores] | ||
(let [tally (tally-scores scores) | ||
low (apply min (keys tally)) | ||
high (apply max (keys tally)) | ||
r (inc (max (Math/abs low) high))] | ||
(doseq [val (range (- r) (inc r))] | ||
(print val) | ||
(print "\t") | ||
(if-let [c (tally val)] | ||
(print (join (repeat c "*")))) | ||
(println) | ||
))) |