Created
July 19, 2018 00:54
-
-
Save CoryFoy/a6279d36afd646c2109fe9480bc227f6 to your computer and use it in GitHub Desktop.
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
;;core-test.clj | |
(ns gol.core-test | |
(:require [midje.sweet :refer :all] | |
[gol.core :refer :all])) | |
(facts "coin changer" | |
(fact "returns a penny for a penny" | |
(make-change-for 1) => [1]) | |
(fact "returns 2 pennies for 2 cents" | |
(make-change-for 2) => [1,1]) | |
(fact "returns a nickel for 5 cents" | |
(make-change-for 5) => [5]) | |
(fact "returns a nickel and a penny for 6 cents" | |
(make-change-for 6) => [5,1]) | |
(fact "returns a dime for 10 cents" | |
(make-change-for 10) => [10]) | |
(fact "returns a quarter for 25 cents" | |
(make-change-for 25) => [25]) | |
(fact "returns 4 quarters for a dollar" | |
(make-change-for 100) => [25,25,25,25]) | |
(fact "returns all the coins" | |
(make-change-for 41) => [25,10,5,1]) | |
) | |
;;================================================================ | |
;;================================================================ | |
;;core.clj | |
(ns gol.core) | |
(defn make-change-for [amount] | |
(def coins [25,10,5,1]) | |
(loop [ | |
coin-num 0 | |
change [] | |
remaining amount] | |
(def coin (get coins coin-num)) | |
(if (= remaining 0) | |
change | |
(if (>= remaining coin) | |
(recur coin-num (conj change coin) (- remaining coin)) | |
(recur (+ coin-num 1) change remaining) | |
) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment