Created
September 2, 2015 23:58
-
-
Save cddr/9a59cec4a9f87f1d0540 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
;; This one doesn't agree with Danny's test-cases but I think it matches the spirit of | |
;; the problem a little better in that it doesn't include an entry for the coin if | |
;; the quantity of that coin would be zero. | |
;; | |
;; Also, we're global, so I used UK coins | |
;; | |
;; So e.g. (change-coins 18) = {10 1, 5 1, 2 1, 1 1} | |
(def coins [100 50 20 10 5 2 1]) | |
(defn change-coins | |
[x] | |
(cond | |
(= x 0) {} | |
:else (let [coin (first (filter #(<= % x) coins)) | |
qty (quot x coin)] | |
(merge | |
{coin qty} | |
(change-coins (- x (* coin qty))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment