Created
December 11, 2009 02:05
-
-
Save francoisdevlin/253922 to your computer and use it in GitHub Desktop.
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
| ;Defined in terms of pennies to avoid float error | |
| (def xkcd | |
| (sorted-map-by | |
| (comp - compare) | |
| 215 :fruit | |
| 275 :fries | |
| 335 :salad | |
| 355 :wings | |
| 420 :cheeze | |
| 580 :sampler)) | |
| ;Assume seq-utils is used | |
| (defn order | |
| ([amt menu] (distinct (flatten (vector (order amt menu {}))))) | |
| ([amt menu items] | |
| (let [affordable (keys-pred drop-while #(< amt %) menu) | |
| exact-entry? (affordable amt)] | |
| (cond | |
| (empty? affordable) {} | |
| exact-entry? (merge-with + items {exact-entry? 1}) | |
| true | |
| (remove empty? | |
| (set | |
| (map | |
| #(let [next-amt (- amt (key %)) | |
| next-items (merge-with + items {(val %) 1})] | |
| (order next-amt affordable next-items)) | |
| affordable))))))) | |
| user=>(order 1505 xkcd) | |
| ({:fruit 1, :wings 2, :sampler 1} {:fruit 7}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment