RosettaCodeData/Task/Knapsack-problem-0-1/Zkl/knapsack-problem-0-1-2.zkl
2017-09-25 22:28:19 +02:00

13 lines
809 B
Text

items:=T(T("apple", 39, 40),T("banana", 27,60), // item: (name,weight,value)
T("beer", 52, 10),T("book", 30,10),T("camera", 32, 30),
T("cheese", 23, 30),T("compass", 13,35),T("glucose", 15, 60),
T("map", 9,150),T("note-case",22,80),T("sandwich", 50,160),
T("socks", 4, 50),T("sunglasses",7,20),T("suntan cream",11, 70),
T("t-shirt", 24, 15),T("tin", 68,45),T("towel", 18, 12),
T("trousers", 48, 10),T("umbrella", 73,40),T("water", 153,200),
T("overclothes",43, 75),T("waterproof trousers",42,70) );
const MAX_WEIGHT=400;
knapsack:=items.reduce(addItem,
(MAX_WEIGHT).pump(List,T(0,T).copy))[-1]; // nearest to max weight
weight:=items.apply('wrap(it){ knapsack[1].holds(it[0]) and it[1] }).sum(0);
knapsack.println(weight);