Skip to content

Commit

Permalink
Add knapsack exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
kahgoh committed Jul 13, 2024
1 parent af7e67e commit 064df65
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "knapsack",
"name": "Knapsack",
"uuid": "5b7094ea-9702-41ca-b13b-c2100cda0bdc",
"practices": [],
"prerequisites": [],
"difficulty": 5
}
]
},
Expand Down
10 changes: 10 additions & 0 deletions exercises/practice/binary/src/example.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(defmodule binary-string
(export (to-decimal 1)))

(defun to-decimal (string)
(try (element 2 (lists:foldr #'to-decimal/2 #(0 0) string))
(catch (_ 0))))

(defun to-decimal
([#\0 `#(,n ,acc)] `#(,(+ n 1) ,acc))
([#\1 `#(,n ,acc)] `#(,(+ n 1) ,(+ acc (trunc (math:pow 2 n))))))
25 changes: 25 additions & 0 deletions exercises/practice/knapsack/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Instructions

Your task is to determine which items to take so that the total value of his selection is maximized, taking into account the knapsack's carrying capacity.

Items will be represented as a list of items.
Each item will have a weight and value.
All values given will be strictly positive.
Bob can take only one of each item.

For example:

```text
Items: [
{ "weight": 5, "value": 10 },
{ "weight": 4, "value": 40 },
{ "weight": 6, "value": 30 },
{ "weight": 4, "value": 50 }
]
Knapsack Maximum Weight: 10
```

For the above, the first item has weight 5 and value 10, the second item has weight 4 and value 40, and so on.
In this example, Bob should take the second and fourth item to maximize his value, which, in this case, is 90.
He cannot get more than 90 as his knapsack has a weight limit of 10.
8 changes: 8 additions & 0 deletions exercises/practice/knapsack/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Introduction

Bob is a thief.
After months of careful planning, he finally manages to crack the security systems of a fancy store.

In front of him are many items, each with a value and weight.
Bob would gladly take all of the items, but his knapsack can only hold so much weight.
Bob has to carefully consider which items to take so that the total value of his selection is maximized.
19 changes: 19 additions & 0 deletions exercises/practice/knapsack/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"kahgoh"
],
"files": {
"solution": [
"src/knapsack.lfe"
],
"test": [
"test/knapsack-tests.lfe"
],
"example": [
".meta/example.lfe"
]
},
"blurb": "Given a knapsack that can only carry a certain weight, determine which items to put in the knapsack in order to maximize their combined value.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Knapsack_problem"
}
55 changes: 55 additions & 0 deletions exercises/practice/knapsack/.meta/example.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(defmodule knapsack
(export (maximum-value 2)
(new-item 2)))

(defrecord item
weight
value
)

(defun new-item (item-weight item-value)
(make-item weight item-weight value item-value))

(defun find-max
(item last-values capacity)
(let*
((value-without (lists:nth (+ capacity 1) last-values))
(weight-value (item-weight item))
(value-value (item-value item))
(left-over-capacity (- capacity weight-value)))
(if
(< left-over-capacity 0)
value-without
(let
((value-with
(+
(lists:nth (+ left-over-capacity 1) last-values)
value-value)))
(if (> value-with value-without) value-with value-without)))))

(defun next-values
([item last-values -1 acc] acc)
([item last-values capacity acc]
(next-values
item
last-values
(- capacity 1)
(cons (find-max item last-values capacity) acc))))

(defun do-maximum-values
([[] last-values capacity] (lists:last last-values))
([(cons item remaining) last-values capacity]
(do-maximum-values
remaining
(next-values item last-values capacity [])
capacity)))

(defun maximum-value
(([] _capacity) 0)
((items capacity)
(do-maximum-values
items
(lists:duplicate
(+ capacity 1)
0)
capacity)))
36 changes: 36 additions & 0 deletions exercises/practice/knapsack/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[a4d7d2f0-ad8a-460c-86f3-88ba709d41a7]
description = "no items"
include = false

[3993a824-c20e-493d-b3c9-ee8a7753ee59]
description = "no items"
reimplements = "a4d7d2f0-ad8a-460c-86f3-88ba709d41a7"

[1d39e98c-6249-4a8b-912f-87cb12e506b0]
description = "one item, too heavy"

[833ea310-6323-44f2-9d27-a278740ffbd8]
description = "five items (cannot be greedy by weight)"

[277cdc52-f835-4c7d-872b-bff17bab2456]
description = "five items (cannot be greedy by value)"

[81d8e679-442b-4f7a-8a59-7278083916c9]
description = "example knapsack"

[f23a2449-d67c-4c26-bf3e-cde020f27ecc]
description = "8 items"

[7c682ae9-c385-4241-a197-d2fa02c81a11]
description = "15 items"
21 changes: 21 additions & 0 deletions exercises/practice/knapsack/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ERL := $(shell which erl)
REBAR3 := $(shell which rebar3)

null :=
space := $(null) #
comma := ,

ifeq ($(ERL),)
$(error Can't find Erlang executable 'erl')
else ifeq ($(REBAR3),)
$(error Can't find rebar3)
endif

compile: ; $(REBAR3) compile

clean: ; $(REBAR3) clean

.PHONY: test
test:
$(REBAR3) eunit \
-m $(subst $(space),$(comma),$(basename $(notdir $(wildcard test/*.lfe))))
11 changes: 11 additions & 0 deletions exercises/practice/knapsack/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{plugins, [{rebar3_lfe, "0.4.10"}]}.

{provider_hooks, [{post, [{compile, {lfe, compile}}]}]}.

{deps, [{lfe, "2.1.3"}]}.

{profiles,
[{test,
[{eunit_compile_opts, [{src_dirs, ["src", "test"]}]},
{deps,
[{ltest, "0.13.8"}]}]}]}.
8 changes: 8 additions & 0 deletions exercises/practice/knapsack/rebar.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{"1.2.0",
[{<<"lfe">>,{pkg,<<"lfe">>,<<"2.1.3">>},0}]}.
[
{pkg_hash,[
{<<"lfe">>, <<"6EFCB2BBC1FFC21DC5D1C092F00EFDB397EAC889474AC5C86EDF78A3557CC730">>}]},
{pkg_hash_ext,[
{<<"lfe">>, <<"4E4BAD515A169AE418FEB7374EA1C8D741FAEA9D95E266CE343B45BCC377F55B">>}]}
].
11 changes: 11 additions & 0 deletions exercises/practice/knapsack/src/knapsack.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%% -*- erlang -*-
{application, 'knapsack',
[{description, "exercism.org - knapsack"},
{vsn, "0.0.1"},
{modules,
['knapsack']},
{registered, []},
{applications,
[kernel, stdlib]},
{included_applications, []},
{env, []}]}.
11 changes: 11 additions & 0 deletions exercises/practice/knapsack/src/knapsack.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(defmodule knapsack
(export (maximum-value 2)
(new-item 2)))

(defun new-item (item-weight item-value)
;; Please implement the "new-item" function
0)

(defun maximum-value (items capacity)
;; Please implement the "maximum-value" function
0)
82 changes: 82 additions & 0 deletions exercises/practice/knapsack/test/knapsack-tests.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
(defmodule knapsack-tests
(behaviour ltest-unit)
(export all))

(include-lib "ltest/include/ltest-macros.lfe")

(deftest no-items
(is-equal 0 (knapsack:maximum-value [] 100)))

(deftest one-item-too-heavy
(is-equal 0
(knapsack:maximum-value
(list
(knapsack:new-item 100 1))
10)))

(deftest five-items-cannot-be-greedy-by-weight
(is-equal 21
(knapsack:maximum-value
(list
(knapsack:new-item 2 5)
(knapsack:new-item 2 5)
(knapsack:new-item 2 5)
(knapsack:new-item 2 5)
(knapsack:new-item 10 21))
10)))

(deftest five-items-cannot-be-greedy-by-value
(is-equal 80
(knapsack:maximum-value
(list
(knapsack:new-item 2 20)
(knapsack:new-item 2 20)
(knapsack:new-item 2 20)
(knapsack:new-item 2 20)
(knapsack:new-item 10 50))
10)))

(deftest example-knapsack
(is-equal 90
(knapsack:maximum-value
(list
(knapsack:new-item 5 10)
(knapsack:new-item 4 40)
(knapsack:new-item 6 30)
(knapsack:new-item 4 50))
10)))

(deftest 8-items
(is-equal 900
(knapsack:maximum-value
(list
(knapsack:new-item 25 350)
(knapsack:new-item 35 400)
(knapsack:new-item 45 450)
(knapsack:new-item 5 20)
(knapsack:new-item 25 70)
(knapsack:new-item 3 8)
(knapsack:new-item 2 5)
(knapsack:new-item 2 5))
104)))

(deftest 15-items
(is-equal 1458
(knapsack:maximum-value
(list
(knapsack:new-item 70 135)
(knapsack:new-item 73 139)
(knapsack:new-item 77 149)
(knapsack:new-item 80 150)
(knapsack:new-item 82 156)
(knapsack:new-item 87 163)
(knapsack:new-item 90 173)
(knapsack:new-item 94 184)
(knapsack:new-item 98 192)
(knapsack:new-item 106 201)
(knapsack:new-item 110 210)
(knapsack:new-item 113 214)
(knapsack:new-item 115 221)
(knapsack:new-item 118 229)
(knapsack:new-item 120 240))
750)))

0 comments on commit 064df65

Please sign in to comment.