Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,8 @@
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
fcn([(v1,_)]a,[(v2,_)]b){ v1>v2 and a or b },new));
},old,nm,w,v,old);
}//--> new list

View file

@ -0,0 +1,14 @@
items:=T( // item: (name,weight,value,#)
T("apple", 39, 40,3),T("banana", 27,60,3),
T("beer", 52, 10,3),T("book", 30,10,2),T("camera", 32, 30,1),
T("cheese", 23, 30,1),T("compass", 13,35,1),T("glucose", 15, 60,2),
T("map", 9,150,1),T("note-case",22,80,1),T("sandwich", 50, 60,2),
T("socks", 4, 50,1),T("sunglasses",7,20,1),T("suntan cream",11, 70,1),
T("t-shirt", 24, 15,2),T("tin", 68,45,3),T("towel", 18, 12,2),
T("trousers", 48, 10,2),T("umbrella", 73,40,1),T("water", 153,200,2),
T("overclothes",43, 75,1),T("waterproof trousers",42,70,1) );
weight:='wrap(knapsack){ // knapsack is (cost, (nm,#,nm,#...))
knapsack[1].pump(List,Void.Read, // read nm,#, first read is implicit
'wrap(nm,n){ items[items.filter1n(fcn(it,nm){ it[0]==nm },nm)][1]*n })
.sum(0)
};

View file

@ -0,0 +1,3 @@
const MAX_WEIGHT=400;
knapsack:=items.reduce(addItem,(MAX_WEIGHT).pump(List,T(0,T).copy))[-1];
knapsack.toString(*).println(weight(knapsack));