Skip to content

Instantly share code, notes, and snippets.

@cddr
Created September 2, 2015 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cddr/9a59cec4a9f87f1d0540 to your computer and use it in GitHub Desktop.
Save cddr/9a59cec4a9f87f1d0540 to your computer and use it in GitHub Desktop.
;; 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