mirror of
https://github.com/rjNemo/exercism-elixir
synced 2026-06-06 02:16:48 +00:00
16 lines
485 B
Elixir
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
|