Skip to content

Commit

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

* Reviewer feedback:

- remove #:transparent from struct definition
- ask dr. racket to reindent test code
  • Loading branch information
blakelewis authored Jan 30, 2024
1 parent 8f65be1 commit fde347a
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "knapsack",
"name": "Knapsack",
"uuid": "e08ec6c5-85bc-492a-b670-51ca95db4352",
"practices": [],
"prerequisites": [],
"difficulty": 3
}
]
},
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.
17 changes: 17 additions & 0 deletions exercises/practice/knapsack/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["blakelewis"],
"files": {
"solution": [
"knapsack.rkt"
],
"test": [
"knapsack-test.rkt"
],
"example": [
".meta/example.rkt"
]
},
"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"
}
17 changes: 17 additions & 0 deletions exercises/practice/knapsack/.meta/example.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#lang racket

(provide item maximum-value)

(struct item (weight value))

(define (maximum-value maximum-weight items)
(for/fold
([top-value (make-vector (add1 maximum-weight) 0)]
#:result (vector-ref top-value maximum-weight))
([it items])
(for/vector ([w (in-inclusive-range 0 maximum-weight)])
(if (> (item-weight it) w)
(vector-ref top-value w)
(max (vector-ref top-value w)
(+ (vector-ref top-value (- w (item-weight it)))
(item-value it)))))))
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"
78 changes: 78 additions & 0 deletions exercises/practice/knapsack/knapsack-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#lang racket/base

(require "knapsack.rkt")

(module+ test
(require rackunit rackunit/text-ui))

(module+ test
(define suite
(test-suite
"knapsack tests"

(test-equal? "no items"
(maximum-value 100 '())
0)

(test-equal? "one item, too heavy"
(maximum-value 10 (list (item 100 1)))
0)

(test-equal? "five items (cannot be greedy by weight)"
(maximum-value 10
(list (item 2 5)
(item 2 5)
(item 2 5)
(item 2 5)
(item 10 21)))
21)

(test-equal? "five items (cannot be greedy by value)"
(maximum-value 10
(list (item 2 20)
(item 2 20)
(item 2 20)
(item 2 20)
(item 10 50)))
80)

(test-equal? "example knapsack"
(maximum-value 10
(list (item 5 10)
(item 4 40)
(item 6 30)
(item 4 50)))
90)

(test-equal? "8 items"
(maximum-value 104
(list (item 25 350)
(item 35 400)
(item 45 450)
(item 5 20)
(item 25 70)
(item 3 8)
(item 2 5)
(item 2 5)))
900)

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

(run-tests suite))
8 changes: 8 additions & 0 deletions exercises/practice/knapsack/knapsack.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#lang racket

(provide item maximum-value)

(struct item (weight value) #:transparent)

(define (maximum-value maximum-weight items)
(error "Not implemented yet"))

0 comments on commit fde347a

Please sign in to comment.