RosettaCodeData/Task/Knapsack-problem-Continuous/11l/knapsack-problem-continuous.11l

30 lines
931 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
V items = [(beef, 3.8, 36.0),
(pork, 5.4, 43.0),
(ham, 3.6, 90.0),
(greaves, 2.4, 45.0),
(flitch, 4.0, 30.0),
(brawn, 2.5, 56.0),
(welt, 3.7, 67.0),
(salami, 3.0, 95.0),
(sausage, 5.9, 98.0)]
V MAXWT = 15.0
V sorted_items = sorted(items.map((name, amount, value) -> (value / amount, amount, name)), reverse' 1B)
V wt = 0.0
V val = 0.0
[(String, Float, Float)] bagged
L(unit_value, amount, name) sorted_items
V portion = min(MAXWT - wt, amount)
wt += portion
V addval = portion * unit_value
val += addval
bagged [+]= (name, portion, addval)
I wt >= MAXWT
L.break
print( ITEM PORTION VALUE)
print(bagged.map((n, p, a) -> #10 #3.2 #3.2.format(n, p, a)).join("\n"))
print("\nTOTAL WEIGHT: #2.2\nTOTAL VALUE: #2.2".format(wt, val))