RosettaCodeData/Task/Knapsack-problem-Bounded/Zkl/knapsack-problem-bounded-1.zkl

9 lines
373 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
fcn addItem(old,[(nm,w,v,c)]){ // old:list:(cost of:,(name,#,...))
[1..c].reduce(fcn(list,i,nm,w,v,old){
wi,left,right:=w*i,list[0,wi],list[wi,*];
new:=old.apply('wrap([(val,itms)]){ T(val + v*i,itms.append(nm,i)) });
left.extend(right.zipWith( // inc
2026-04-30 12:34:36 -04:00
fcn([(v1,_)]a,[(v2,_)]b){ v1>v2 and a or b },new));
2023-07-01 11:58:00 -04:00
},old,nm,w,v,old);
}//--> new list