Skip to content

Instantly share code, notes, and snippets.

@nodename
Created February 27, 2012 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodename/1921676 to your computer and use it in GitHub Desktop.
Save nodename/1921676 to your computer and use it in GitHub Desktop.
HW6-breaking-dollars failed solution
(ns breaking-dollars)
(defn first-coin-choices [cents denominations already-chosen]
(let [possible-choices (filter #(<= % cents) denominations)]
(if (empty? already-chosen)
possible-choices
; choose descending order to eliminate permutations:
(filter #(<= % (last already-chosen)) possible-choices))))
(defn totalling-cents
([cents denominations]
(totalling-cents cents denominations []))
([cents denominations already-chosen]
(let [new-choices (first-coin-choices cents denominations already-chosen)]
(if (empty? new-choices)
already-chosen
; (totalling-cents cents denominations already-chosen (first new-choices)))))
(map #(totalling-cents cents denominations already-chosen %) new-choices))))
([cents denominations already-chosen new-choice]
(let [new-list (conj already-chosen new-choice)
; _ (println "already:" already-chosen)
; _ (println "new lists:" new-lists)
answer (totalling-cents (- cents new-choice) denominations new-list)]
; (println "answer:" answer)
answer)))
(defn breaking-dollars [dollars denominations]
(totalling-cents (* dollars 100) denominations))
(ns breaking-dollars-spec
(:use breaking-dollars)
(:use clojure.test))
(deftest breaking-dollars-test
(let [dollars 1
denominations [50 25]
combinations (breaking-dollars dollars denominations)
; _ (println ":::")
_ (println combinations)
]
(is (= (count combinations) 3))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment