Knapsack problem/Continuous: Difference between revisions

m
Line 578:
solution k = loop k . sortBy (flip $ comparing unitWeight)
where loop k ((name, (_, weight)):xs)
| weight < k = [putStrLn ("Take all the " ++ name]) ++>> loop (k-weight) xs
| otherwise = [printf "Take %.2f kg of the %s\n" (k :: Float) name]
 
main = mapM_ putStrLn $ solution 15 items
</lang>
 
Anonymous user