Commit 64eb339b authored by Andreas Pfurtscheller's avatar Andreas Pfurtscheller

[HW09] Added knapsack algorithm

parent e2db83a2
......@@ -108,9 +108,14 @@ let run_santas_factory c a =
(* 9.3 - 7 *)
let knapsack = todo
let knapsack l c =
let comp = fun (_,i1,_) (_,i2,_) -> (if i1 > i2 then -1 else if i1 < i2 then 1 else 0) in
let sorted = List.sort comp l in
let list_weight list = List.fold_left (fun tw (_,_,w) -> tw + w) 0 list in
let reduce list (n,i,w) =
let weight = list_weight list in
if (weight + w) <= c then (n,i,w)::list else list in
List.rev_map (fun (n,_,_) -> n) (List.fold_left reduce [] sorted)
(*****************************************************************************)
(**************************** END OF HOMEWORK ********************************)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment