Skip to content

Instantly share code, notes, and snippets.

@tmountain
Created January 7, 2010 19:59
Show Gist options
  • Save tmountain/271513 to your computer and use it in GitHub Desktop.
Save tmountain/271513 to your computer and use it in GitHub Desktop.
(defn all-permutations [things]
(if (= 1 (count things))
(list things)
(for [head things
tail (all-permutations (disj (set things) head))]
(do
(cons head tail)))))
(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))
(defn combo-matches-price? [target combo accum]
(if (= target (+ (second combo) accum))
true
(if (zero? (count (rest combo)))
false
(recur target (rest combo) (+ (second combo) accum)))))
(doseq [combo combos]
(if (combo-matches-price? target (first 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