Knapsack problem/Continuous: Difference between revisions

Line 373:
(defn rob [items capacity]
(let [best-items (reverse (sort-by per-kg items))]
(loop [items best-items roomcap capacity total 0 instr ""]
(let [x (first items)]
(if (< (:weight x) roomcap)
(recur (rest items)
(- roomcap (:weight x))
(+ total (:price x))
(str instr "Take all " (:name x) "\n"))
(format "%sTake %.1f kg of %s\nTotal: %.2f"
instr roomcap (:name x) (+ total (* roomcap (per-kg x)))))))))
 
(print (rob items 15))