Created
September 25, 2012 01:42
-
-
Save mjansen401/3779498 to your computer and use it in GitHub Desktop.
Coin Changer solution in Clojure
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
(ns coin-changer.core) | |
(defn can-use-coin? [coin amount] | |
(<= coin amount)) | |
(defn largest-coin [amount] | |
(some #(if (can-use-coin? % amount) %) [25 10 5 1])) | |
(defn change-for [n] | |
(loop [amount n coins []] | |
(if (= 0 amount) | |
coins | |
(recur (- amount (largest-coin amount)) (conj coins (largest-coin amount)))))) | |
----- | |
(ns coin-changer.core-spec | |
(:require [speclj.core :refer :all] | |
[coin-changer.core :refer :all])) | |
(describe "coin changer" | |
(it "changes 0 as []" | |
(should= [] (change-for 0))) | |
(it "changes 1 as [1]" | |
(should= [1] (change-for 1))) | |
(it "changes 2 as [1 1]" | |
(should= [1 1] (change-for 2))) | |
(it "changes 3 as [1 1 1]" | |
(should= [1 1 1] (change-for 3))) | |
(it "changes 5 as [5]" | |
(should= [5] (change-for 5))) | |
(it "changes 6 as [5 1]" | |
(should= [5 1] (change-for 6))) | |
(it "changes 10 as [10]" | |
(should= [10] (change-for 10))) | |
(it "changes 25 as [25]" | |
(should= [25] (change-for 25))) | |
(it "changes 99 as [25 25 25 10 10 1 1 1 1]" | |
(should= [25 25 25 10 10 1 1 1 1] (change-for 99))) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment