exercism-elixir/knapsack/lib/knapsack.ex
2022-02-02 11:34:13 -04:00

16 lines
485 B
Elixir

defmodule Knapsack do
@doc """
Return the maximum value that a knapsack can carry.
"""
@spec maximum_value(items :: [%{value: integer, weight: integer}], maximum_weight :: integer) ::
integer
def maximum_value([], _), do: 0
def maximum_value([%{weight: weight}], maximum_weight)
when weight > maximum_weight,
do: 0
def maximum_value(items, maximum_weight) do
items |> Enum.filter(fn %{weight: weight} -> weight < maximum_weight end)
end
end