Created
January 15, 2013 05:47
-
-
Save nopolabs/4536504 to your computer and use it in GitHub Desktop.
greedy change
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
(ns coin.core) | |
(def no-change {1 0 5 0 10 0 25 0}) | |
(def coins [25 10 5 1]) | |
(defn count-change | |
[change] | |
(reduce | |
(fn [value coin] | |
(let [n-coins (get change coin)] | |
(+ (* n-coins coin) value))) | |
0 | |
(keys change))) | |
(defn make-change | |
[amount] | |
(first | |
(reduce | |
(fn [[chg amt] coin] | |
(let [n-coins (quot amt coin) | |
remaining-amt (rem amt coin)] | |
[(assoc chg coin n-coins) remaining-amt])) | |
[no-change amount] | |
coins))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment