Knapsack problem/Continuous: Difference between revisions
Content added Content deleted
Line 371: | Line 371: | ||
(defn per-kg [item] (/ (:price item) (:weight item))) |
(defn per-kg [item] (/ (:price item) (:weight item))) |
||
(defn rob |
(defn rob |
||
"Returns a string of instructions for what valuables to steal from a collection |
|||
of /items/ with the map keys: name, weight, and price, given a limited carrying /capacity/!. |
|||
First we sort our list so the first item (x) is the most valuable, then we |
|||
continuously pop off the first item into the (instr)uctions string until our |
|||
remaining carrying (cap)acity can't hold the full weight of the last item." |
|||
[items capacity] |
|||
(let [best-items (reverse (sort-by per-kg items))] |
(let [best-items (reverse (sort-by per-kg items))] |
||
(loop [items best-items cap capacity total 0 instr ""] |
(loop [items best-items cap capacity total 0 instr ""] |