RosettaCodeData/Task/Knapsack-problem-0-1/Zkl/knapsack-problem-0-1-1.zkl
2023-07-01 13:44:08 -04:00

6 lines
309 B
Text

fcn addItem(pairs,it){ // pairs is list of (cost of:,names), it is (name,w,v)
w,left,right:=it[1],pairs[0,w],pairs[w,*];
left.extend(right.zipWith(
fcn([(t1,_)]a,[(t2,_)]b){ t1>t2 and a or b },
pairs.apply('wrap([(tot,names)]){ T(tot + it[2], names + it[0]) })))
}//--> new list of pairs