Last active
December 20, 2015 09:48
-
-
Save jaked/6110141 to your computer and use it in GitHub Desktop.
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 count-ways-to-make-change [coins sum] | |
(defn sum-counts [counts] | |
(reduce + (map * coins counts))) | |
(defn next-counts [counts] | |
(loop [counts counts | |
digit 0] | |
(if (>= digit (count counts)) | |
nil | |
(let [next (assoc counts digit (inc (get counts digit)))] | |
(if (<= (sum-counts next) sum) | |
next | |
(recur (assoc counts digit 0) (inc digit))))))) | |
(loop [counts (vec (map (constantly 0) coins)) | |
ways 0] | |
(if (nil? counts) | |
ways | |
(let [sum-counts (sum-counts counts) | |
ways (if (= sum-counts sum) (inc ways) ways) | |
counts (next-counts counts)] | |
(recur counts ways))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment