Skip to content

Instantly share code, notes, and snippets.

@gigasquid
Created December 5, 2012 14:03
Show Gist options
  • Save gigasquid/4215732 to your computer and use it in GitHub Desktop.
Save gigasquid/4215732 to your computer and use it in GitHub Desktop.
Code and Coffee - 12/5/2012
defn change-for [change coins returning]
(let [ coin (first coins)
others (rest coins)
]
(cond
(= change 0) returning
(>= change coin) (change-for (mod change coin) others (conj returning {coin (int (/ change coin))}))
:else (change-for change others returning))))
(defn make-change [price tendered]
(let [ change (- tendered price) ]
(change-for change [25 10 5 1] {})))
(= (make-change 125 200) {25 3})
(make-change 124 200)
(= (make-change 124 200) {25 3, 1 1})
(make-change 126 200)
(= (make-change 126 200) {25 2, 10 2, 1 4})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment