Skip to content

Instantly share code, notes, and snippets.

@adomokos adomokos/change_maker.clj
Last active Jan 14, 2016

Embed
What would you like to do?
The Change Maker Kata - http://rubyquiz.com/quiz154.html
(ns change-maker.core-test
(:require [clojure.test :refer :all]))
(def ^:dynamic *denominations* [25 10 5 1])
(defn best-match [value]
(first (filter #(<= % value) *denominations*)))
(defn calculate-change [value product]
(let [reducer (best-match value)]
(if (= 0 value)
product
(recur (- value reducer)
(conj product reducer)))))
(defn denoms []
(loop [coll *denominations*
result []]
(if (empty? coll)
result
(recur (rest coll) (conj result coll)))))
(defn permutations [value]
(map #(binding [*denominations* %]
(calculate-change value []))
(denoms)))
(defn least-number-of-coins [variations]
(first (sort-by count variations)))
(defn make-change [value]
(let [results (permutations value)]
(least-number-of-coins results)))
(deftest change-maker
(are [x y] (= x (make-change y))
[25] 25
[25 10 10] 45
[25 10 1 1 1 1] 39)
(binding [*denominations* [10 7 1]]
(are [x y] (= x (make-change y))
[10 7] 17
[7 7] 14)))
(deftest denoms-test
(binding [*denominations* [3 2 1]]
(is (= [[3 2 1] [2 1] [1]] (denoms)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.