Skip to content

Commit

Permalink
Add knapsack exercise (#377)
Browse files Browse the repository at this point in the history
* Add knapsack exercise

* Use association lists to represet items.
  • Loading branch information
keiravillekode authored May 11, 2024
1 parent e513134 commit 7078902
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,17 @@
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "knapsack",
"name": "Knapsack",
"uuid": "41029de7-58a6-431b-9f82-6d4603604fb3",
"practices": [],
"prerequisites": [],
"difficulty": 7,
"topics": [
"control_flow_loops"
]
}
]
},
Expand Down
35 changes: 35 additions & 0 deletions exercises/practice/knapsack/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Instructions

In this exercise, let's try to solve a classic problem.

Bob is a thief.
After months of careful planning, he finally manages to crack the security systems of a high-class apartment.

In front of him are many items, each with a value (v) and weight (w).
Bob, of course, wants to maximize the total value he can get; he would gladly take all of the items if he could.
However, to his horror, he realizes that the knapsack he carries with him can only hold so much weight (W).

Given a knapsack with a specific carrying capacity (W), help Bob determine the maximum value he can get from the items in the house.
Note that Bob can take only one of each item.

All values given will be strictly positive.
Items will be represented as a list of items.
Each item will have a weight and value.

For example:

```none
Items: [
{ "weight": 5, "value": 10 },
{ "weight": 4, "value": 40 },
{ "weight": 6, "value": 30 },
{ "weight": 4, "value": 50 }
]
Knapsack Limit: 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.
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": [
"keiravillekode"
],
"files": {
"solution": [
"knapsack.el"
],
"test": [
"knapsack-test.el"
],
"example": [
".meta/example.el"
]
},
"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"
}
22 changes: 22 additions & 0 deletions exercises/practice/knapsack/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;;; knapsack.el --- Knapsack (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'cl-lib)

(defun maximum-value (maximum-weight items)
(let ((table (make-vector (1+ maximum-weight) 0)))
(dolist (item items)
(let ((weight (alist-get :weight item))
(value (alist-get :value item)))
(cl-loop for index from maximum-weight downto weight
for new-value = (+ value (aref table (- index weight)))
do (when (> new-value (aref table index))
(aset table index new-value)))))
(aref table maximum-weight)))

(provide 'knapsack)
;;; knapsack.el ends here

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"
73 changes: 73 additions & 0 deletions exercises/practice/knapsack/knapsack-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
;;; knapsack-test.el --- Tests for Knapsack (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(load-file "knapsack.el")
(declare-function maximum-value "knapsack.el" (maximum-weight items))


(ert-deftest no-items ()
(should (= 0 (maximum-value 100 '()))))


(ert-deftest one-item-too-heavy ()
(should (= 0 (maximum-value 10 '(((:weight . 100) (:value . 1)))))))


(ert-deftest five-items-cannot-be-greedy-by-weight ()
(should (= 21 (maximum-value 10 '(((:weight . 2) (:value . 5))
((:weight . 2) (:value . 5))
((:weight . 2) (:value . 5))
((:weight . 2) (:value . 5))
((:weight . 10) (:value . 21)))))))


(ert-deftest five-items-cannot-be-greedy-by-value ()
(should (= 80 (maximum-value 10 '(((:weight . 2) (:value . 20))
((:weight . 2) (:value . 20))
((:weight . 2) (:value . 20))
((:weight . 2) (:value . 20))
((:weight . 10) (:value . 50)))))))


(ert-deftest example-knapsack ()
(should (= 90 (maximum-value 10 '(((:weight . 5) (:value . 10))
((:weight . 4) (:value . 40))
((:weight . 6) (:value . 30))
((:weight . 4) (:value . 50)))))))


(ert-deftest 8-items ()
(should (= 900 (maximum-value 104 '(((:weight . 25) (:value . 350))
((:weight . 35) (:value . 400))
((:weight . 45) (:value . 450))
((:weight . 5) (:value . 20))
((:weight . 25) (:value . 70))
((:weight . 3) (:value . 8))
((:weight . 2) (:value . 5))
((:weight . 2) (:value . 5)))))))


(ert-deftest 15-items ()
(should (= 1458 (maximum-value 750 '(((:weight . 70) (:value . 135))
((:weight . 73) (:value . 139))
((:weight . 77) (:value . 149))
((:weight . 80) (:value . 150))
((:weight . 82) (:value . 156))
((:weight . 87) (:value . 163))
((:weight . 90) (:value . 173))
((:weight . 94) (:value . 184))
((:weight . 98) (:value . 192))
((:weight . 106) (:value . 201))
((:weight . 110) (:value . 210))
((:weight . 113) (:value . 214))
((:weight . 115) (:value . 221))
((:weight . 118) (:value . 229))
((:weight . 120) (:value . 240)))))))


(provide 'knapsack-test)
;;; knapsack-test.el ends here
14 changes: 14 additions & 0 deletions exercises/practice/knapsack/knapsack.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;;; knapsack.el --- Knapsack (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defun maximum-value (maximum-weight items)
(error "Delete this S-Expression and write your own implementation"))


(provide 'knapsack)
;;; knapsack.el ends here

0 comments on commit 7078902

Please sign in to comment.