Icon representing a recipe

Recipe: AILEARNS KNAPSACK TEST

created by Vincera

Profile


Name
AILEARNS KNAPSACK TEST
ID
104683
Shared with
Public
Parent
None
Children
None
Created on
March 24, 2021 at 20:12 PM UTC
Updated on
December 30, 2024 at 19:56 PM UTC
Description

...

Best for


Code


local mymodule = {} local function compare(a, b) return a[4] < b[4] end function mymodule.resolve(items, capacity) local myarray = {} for i=1, #items, 1 do myarray[i] = items[i] local weight = items[i][2] local value = items[i][3] local wpv = weight / value myarray[i][4] = wpv end table.sort(myarray, compare) local response = {} for i=1, #myarray, 1 do local item = myarray[i] capacity = capacity - item[2] if capacity < 0 then break end response[i] = {item[1], item[2], item[3]} end return response end return mymodule

Comments