Skip to content

Commit

Permalink
Knapsack (#765)
Browse files Browse the repository at this point in the history
* add practice exercise knapsack

* fixed typo in a comment
  • Loading branch information
colinleach committed Sep 3, 2024
1 parent d1990b4 commit f666df6
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "knapsack",
"name": "Knapsack",
"uuid": "7c4ecd30-dbd0-489e-a68c-513e9ecb12d9",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "game-of-life",
"name": "Conway's Game of Life",
Expand Down
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": [
"colinleach"
],
"files": {
"solution": [
"knapsack.jl"
],
"test": [
"runtests.jl"
],
"example": [
".meta/example.jl"
]
},
"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"
}
27 changes: 27 additions & 0 deletions exercises/practice/knapsack/.meta/example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function maximum_value(maximum_weight, items)
# See https://en.wikipedia.org/wiki/Knapsack_problem#Dynamic_programming_in-advance_algorithm

numitems = length(items)
numitems == 0 && return 0

# Remember, Julia uses 1-based array indexing
# so maxes[r,c] indexing needs to be r+1, c+1, compared to the Wiki reference

maxes = zeros(UInt16, numitems + 1, maximum_weight + 1)
for item_count in 1:numitems
for weight in 1:(maximum_weight)
curr_weight = items[item_count].weight
if curr_weight > weight
# copy the row above, the new item is no use to us
maxes[item_count + 1, weight + 1] = maxes[item_count, weight + 1]
else
# perhaps drop an old item, add the new one?
replace_last = maxes[item_count, weight - curr_weight + 1] + items[item_count].value
maxes[item_count + 1, weight + 1] = max(maxes[item_count, weight + 1], replace_last)
end
end
end

# converting to signed int is not essential, but it can help with debugging
Int(maxes[end, end])
end
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"
3 changes: 3 additions & 0 deletions exercises/practice/knapsack/knapsack.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function maximum_value(maximum_weight, items)

end
47 changes: 47 additions & 0 deletions exercises/practice/knapsack/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Test

include("knapsack.jl")

struct Item
weight
value
end

@testset verbose = true "tests" begin
@testset "no items" begin
@test maximum_value(100, []) == 0
end

@testset "one item, too heavy" begin
@test maximum_value(10, [Item(100, 1),]) == 0
end

@testset "five items (cannot be greedy by weight)" begin
items = [Item(2, 5), Item(2, 5), Item(2, 5), Item(2, 5), Item(10, 21)]
@test maximum_value(10, items) == 21
end

@testset "five items (cannot be greedy by value)" begin
items = [Item(2, 20), Item(2, 20), Item(2, 20), Item(2, 20), Item(10, 50)]
@test maximum_value(10, items) == 80
end

@testset "example knapsack" begin
items = [Item(5, 10), Item(4, 40), Item(6, 30), Item(4, 50)]
@test maximum_value(10, items) == 90
end

@testset "8 items" begin
items = [Item(25, 350), Item(35, 400), Item(45, 450), Item(5, 20),
Item(25, 70), Item(3, 8), Item(2, 5), Item(2, 5)]
@test maximum_value(104, items) == 900
end

@testset "15 items" begin
items = [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)]
@test maximum_value(750, items) == 1458
end
end

0 comments on commit f666df6

Please sign in to comment.