Skip to content

Commit a433a6e

Browse files
Add knapsack exercise (#57)
1 parent 049d1e0 commit a433a6e

File tree

8 files changed

+153
-0
lines changed

8 files changed

+153
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,14 @@
342342
"prerequisites": [],
343343
"difficulty": 5
344344
},
345+
{
346+
"slug": "knapsack",
347+
"name": "Knapsack",
348+
"uuid": "e35d9981-453c-444d-b7a4-7c8646d0b2d0",
349+
"practices": [],
350+
"prerequisites": [],
351+
"difficulty": 5
352+
},
345353
{
346354
"slug": "nth-prime",
347355
"name": "Nth Prime",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Instructions
2+
3+
Your task is to determine which items to take so that the total value of her selection is maximized, taking into account the knapsack's carrying capacity.
4+
5+
Items will be represented as a list of items.
6+
Each item will have a weight and value.
7+
All values given will be strictly positive.
8+
Lhakpa can take only one of each item.
9+
10+
For example:
11+
12+
```text
13+
Items: [
14+
{ "weight": 5, "value": 10 },
15+
{ "weight": 4, "value": 40 },
16+
{ "weight": 6, "value": 30 },
17+
{ "weight": 4, "value": 50 }
18+
]
19+
20+
Knapsack Maximum Weight: 10
21+
```
22+
23+
For the above, the first item has weight 5 and value 10, the second item has weight 4 and value 40, and so on.
24+
In this example, Lhakpa should take the second and fourth item to maximize her value, which, in this case, is 90.
25+
She cannot get more than 90 as her knapsack has a weight limit of 10.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Introduction
2+
3+
Lhakpa is a [Sherpa][sherpa] mountain guide and porter.
4+
After months of careful planning, the expedition Lhakpa works for is about to leave.
5+
She will be paid the value she carried to the base camp.
6+
7+
In front of her are many items, each with a value and weight.
8+
Lhakpa would gladly take all of the items, but her knapsack can only hold so much weight.
9+
10+
[sherpa]: https://en.wikipedia.org/wiki/Sherpa_people#Mountaineering
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"knapsack.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
14+
]
15+
},
16+
"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.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Knapsack_problem"
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def maximum_value [n] (weights: [n]i32) (values: [n]i32) (maximum_weight: i32): i32 =
2+
let a = loop a = replicate (i64.i32 maximum_weight + 1) 0 for i in 0..<n do
3+
let weight = weights[i]
4+
let value = values[i]
5+
in
6+
if weight > maximum_weight then a else
7+
let a2 = loop a2 = a for j in 0...(maximum_weight - weight) do
8+
let update = a2[j + weight] + value
9+
in
10+
if update <= a2[j] then a2 else
11+
a2 with [j] = update
12+
in
13+
a2
14+
in
15+
a[0]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[a4d7d2f0-ad8a-460c-86f3-88ba709d41a7]
13+
description = "no items"
14+
include = false
15+
16+
[3993a824-c20e-493d-b3c9-ee8a7753ee59]
17+
description = "no items"
18+
reimplements = "a4d7d2f0-ad8a-460c-86f3-88ba709d41a7"
19+
20+
[1d39e98c-6249-4a8b-912f-87cb12e506b0]
21+
description = "one item, too heavy"
22+
23+
[833ea310-6323-44f2-9d27-a278740ffbd8]
24+
description = "five items (cannot be greedy by weight)"
25+
26+
[277cdc52-f835-4c7d-872b-bff17bab2456]
27+
description = "five items (cannot be greedy by value)"
28+
29+
[81d8e679-442b-4f7a-8a59-7278083916c9]
30+
description = "example knapsack"
31+
32+
[f23a2449-d67c-4c26-bf3e-cde020f27ecc]
33+
description = "8 items"
34+
35+
[7c682ae9-c385-4241-a197-d2fa02c81a11]
36+
description = "15 items"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def maximum_value [n] (weights: [n]i32) (values: [n]i32) (maximum_weight: i32): i32 = ???

exercises/practice/knapsack/test.fut

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import "knapsack"
2+
3+
-- no items
4+
-- ==
5+
-- input { empty([0]i32) empty([0]i32) 100 }
6+
-- output { 0 }
7+
8+
-- one item, too heavy
9+
-- ==
10+
-- input { [100] [1] 10 }
11+
-- output { 0 }
12+
13+
-- five items (cannot be greedy by weight)
14+
-- ==
15+
-- input { [2, 2, 2, 2, 10] [5, 5, 5, 5, 21] 10 }
16+
-- output { 21 }
17+
18+
-- five items (cannot be greedy by value)
19+
-- ==
20+
-- input { [2, 2, 2, 2, 10] [20, 20, 20, 20, 50] 10 }
21+
-- output { 80 }
22+
23+
-- example knapsack
24+
-- ==
25+
-- input { [5, 4, 6, 4] [10, 40, 30, 50] 10 }
26+
-- output { 90 }
27+
28+
-- 8 items
29+
-- ==
30+
-- input { [25, 35, 45, 5, 25, 3, 2, 2] [350, 400, 450, 20, 70, 8, 5, 5] 104 }
31+
-- output { 900 }
32+
33+
-- 15 items
34+
-- ==
35+
-- input { [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] 750 }
36+
-- output { 1458 }
37+
38+
def main [n] (weights: [n]i32) (values: [n]i32) (maximum_weight: i32): i32 =
39+
maximum_value weights values maximum_weight

0 commit comments

Comments
 (0)