Skip to content

Instantly share code, notes, and snippets.

@ghadishayban
Created May 1, 2013 21:07
Show Gist options
  • Save ghadishayban/5498369 to your computer and use it in GitHub Desktop.
Save ghadishayban/5498369 to your computer and use it in GitHub Desktop.
Coin Kata for Paul DG
(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))
@ohpauleez
Copy link

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:

FAIL in (kata-test) (core_test.clj:11)
Sub-dollar amounts with #{1 5 10 25}
expected: (= (coin-f denominations 55) {1 0, 5 1, 10 0, 25 2})
  actual: (not (= {5 1, 10 0, 25 2} {1 0, 5 1, 10 0, 25 2}))

FAIL in (kata-test) (core_test.clj:11)
Sub-dollar amounts with #{1 5 10 25}
expected: (= (coin-f denominations 80) {1 0, 5 1, 10 0, 25 3})
  actual: (not (= {5 1, 10 0, 25 3} {1 0, 5 1, 10 0, 25 3}))

FAIL in (kata-test) (core_test.clj:11)
Sub-dollar amounts with #{1 5 10 25}
expected: (= (coin-f denominations 0) {1 0, 5 0, 10 0, 25 0})
  actual: (not (= {25 0} {1 0, 5 0, 10 0, 25 0}))

FAIL in (kata-test) (core_test.clj:17)
Sub-dollar amounts with #{1 20 25}
expected: (= (coin-f denominations 80) {1 0, 20 4, 25 0})
  actual: (not (= {1 5, 20 0, 25 3} {1 0, 20 4, 25 0}))

FAIL in (kata-test) (core_test.clj:17)
Sub-dollar amounts with #{1 20 25}
expected: (= (coin-f denominations 0) {1 0, 20 0, 25 0})
  actual: (not (= {25 0} {1 0, 20 0, 25 0}))

FAIL in (kata-test) (core_test.clj:23)
Sub-dollar amounts with #{1 20 25 75}
expected: (= (coin-f denominations 80) {1 0, 20 4, 25 0, 75 0})
  actual: (not (= {1 5, 20 0, 25 0, 75 1} {1 0, 75 0, 20 4, 25 0}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment