Created
January 7, 2010 20:09
-
-
Save tmountain/271525 to your computer and use it in GitHub Desktop.
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
(defn all-permutations [things] | |
(if (= 1 (count things)) | |
(list things) | |
(for [head things | |
tail (all-permutations (disj (set things) head))] | |
(do | |
(cons head tail))))) | |
(defn combo-matches-price? [target combo accum] | |
(if (= target (+ (second (first combo)) accum)) | |
true | |
(if (zero? (count (rest combo))) | |
false | |
(recur target (rest combo) (+ (second (first combo)) accum))))) | |
(def items {"mixed fruit" 2.15, | |
"french fries" 2.75, | |
"side salad" 3.35, | |
"hot wings" 3.55, | |
"mozzarella sticks" 4.20, | |
"sampler plate" 5.80, | |
"barbecue" 6.55}) | |
(def target 15.05) | |
(def combos (all-permutations items)) | |
(doseq [combo combos] | |
(if (combo-matches-price? target combo 0) | |
(println "Combo " (pr-str combo) " matches"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment