Created
March 28, 2019 01:54
-
-
Save camsaul/b4c1ed2b4ebe70f613edcef4830246c0 to your computer and use it in GitHub Desktop.
break-coins.clj
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 my-coins-project.core) | |
;; Return all the different ways you can make change for a given amount of cents. | |
;; Basic idea is to start with number of pennies and then successively apply `apply-coin` for each coin in order from | |
;; largest to smallest. `apply-coin` will take a lazy seq of existing combinations and return a new lazy seq that includes | |
;; a new combination with each possible quantity of the coin in question for each existing combination. | |
;; | |
;; (apply-coin [:quarter 25] [{:penny 60}]) | |
;; ;; -> [{:quarter 0, :penny 60}, {:quarter 1, :penny 35}, {:quarter 2, :penny 10}] | |
;; | |
;; (apply-coin [:dime 10] [{:quarter 0, :penny 60}, {:quarter 1, :penny 35}, {:quarter 2, :penny 10}]) | |
;; ;; -> [{:quarter 0, :dime 0, :penny 60} {:quarter 0, :dime 1, :penny 50} ...] | |
(def ^:private coins | |
[[:quarter 25] [:dime 10] [:nickel 5]]) | |
(defn- apply-coin [[coin-name coin-value] ms] | |
(for [{:keys [penny], :as m} ms | |
total-value (range 0 (inc penny) coin-value) | |
:let [quantity (/ total-value coin-value)]] | |
(assoc m coin-name quantity, :penny (- penny total-value)))) | |
(defn- change [amount] | |
(loop [[coin & more] coins, acc [{:penny amount}]] | |
(let [acc (apply-coin coin acc)] | |
(if-not (seq more) | |
acc | |
(recur more acc))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Version that works without pennies