Skip to content

Commit

Permalink
Add knapsack exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed May 10, 2024
1 parent e5ca36b commit e14e635
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "knapsack",
"name": "Knapsack",
"uuid": "41029de7-58a6-431b-9f82-6d4603604fb3",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
},
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"
}
23 changes: 23 additions & 0 deletions exercises/practice/knapsack/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
;;; knapsack.el --- Knapsack (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'cl-lib)

(defun knapsack (capacity weights values)
(let ((table (make-vector (1+ capacity) 0)))
(cl-loop while weights
for weight = (car weights)
for value = (car values)
do (setq weights (cdr weights))
do (setq values (cdr values))
do (cl-loop for index from capacity downto weight
do (aset table index (max (aref table index)
(+ value (aref table (- index weight)))))))
(aref table capacity)))

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

;;; Commentary:

;;; Code:

(load-file "knapsack.el")
(declare-function knapsack "knapsack.el" (capacity weights values))


(ert-deftest no-items ()
(should (equal 0 (knapsack 100 '() '()))))


(ert-deftest one-item-too-heavy ()
(should (equal 0 (knapsack 10 '(100) '(1)))))


(ert-deftest five-items-cannot-be-greedy-by-weight ()
(should (equal 21 (knapsack 10 '(2 2 2 2 10) '(5 5 5 5 21)))))


(ert-deftest five-items-cannot-be-greedy-by-value ()
(should (equal 80 (knapsack 10 '(2 2 2 2 10) '(20 20 20 20 50)))))


(ert-deftest example-knapsack ()
(should (equal 90 (knapsack 10 '(5 4 6 4) '(10 40 30 50)))))


(ert-deftest 8-items ()
(should (equal 900 (knapsack 104
'(25 35 45 5 25 3 2 2)
'(350 400 450 20 70 8 5 5)))))


(ert-deftest 15-items ()
(should (equal 1458 (knapsack 750
'(70 73 77 80 82 87 90 94 98 106 110 113 115 118 120)
'(135 139 149 150 156 163 173 184 192 201 210 214 221 229 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 knapsack (capacity weights values)
(error "Delete this S-Expression and write your own implementation"))


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

0 comments on commit e14e635

Please sign in to comment.