Skip to content

Instantly share code, notes, and snippets.

@nopolabs
Created January 15, 2013 05:47
Show Gist options
  • Save nopolabs/4536504 to your computer and use it in GitHub Desktop.
Save nopolabs/4536504 to your computer and use it in GitHub Desktop.
greedy change
(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