Created
May 1, 2013 21:07
-
-
Save ghadishayban/5498369 to your computer and use it in GitHub Desktop.
Coin Kata for Paul DG
This file contains hidden or 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
(ns cljdojo.coin | |
(:require [clojure.test :refer :all])) | |
(def coins (sorted-set-by > 50 25 10 5 1)) | |
(defn min-coins [total] | |
(letfn [(step [[remaining tallies] coin] | |
(let [decrements (->> (iterate #(- % coin) remaining) | |
(take-while #(>= % coin)) | |
count) | |
remainder (- remaining (* coin decrements)) | |
new-tallies (assoc tallies coin decrements)] | |
(if (= 0 remainder) | |
(reduced new-tallies) ;; we're done | |
[remainder new-tallies])))] | |
(reduce step [total {}] coins))) | |
(deftest roundtrip | |
(are [total] | |
(= total | |
(reduce-kv (fn [sum coin count] | |
(+ sum (* coin count))) | |
0 | |
(min-coins total))) | |
44 88 100 25 37)) | |
(comment (roundtrip)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I modified min-coins to take in the coins/denomination arg that it applies
sorted-set-by
and ran it through my test suite. Here are some of the errors: