Created
December 5, 2012 14:03
-
-
Save gigasquid/4215732 to your computer and use it in GitHub Desktop.
Code and Coffee - 12/5/2012
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
defn change-for [change coins returning] | |
(let [ coin (first coins) | |
others (rest coins) | |
] | |
(cond | |
(= change 0) returning | |
(>= change coin) (change-for (mod change coin) others (conj returning {coin (int (/ change coin))})) | |
:else (change-for change others returning)))) | |
(defn make-change [price tendered] | |
(let [ change (- tendered price) ] | |
(change-for change [25 10 5 1] {}))) | |
(= (make-change 125 200) {25 3}) | |
(make-change 124 200) | |
(= (make-change 124 200) {25 3, 1 1}) | |
(make-change 126 200) | |
(= (make-change 126 200) {25 2, 10 2, 1 4}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment