Skip to content

Commit

Permalink
Merge pull request #1484 from rrtheonlyone/knapsack-exercise
Browse files Browse the repository at this point in the history
Knapsack: Proposal for new exercise
  • Loading branch information
kotp authored Mar 28, 2019
2 parents 5b29a25 + d12c20d commit 3562b95
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
116 changes: 116 additions & 0 deletions exercises/knapsack/canonical-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"exercise": "knapsack",
"version": "1.0.0",
"comments": [
"Depending on the language, the input type can be modified"
],
"cases": [
{
"description": "no items",
"property": "maximumValue",
"input": {
"maximumWeight": 100,
"items": {}
},
"expected": 0
},
{
"description": "one item, too heavy",
"property": "maximumValue",
"input": {
"maximumWeight": 10,
"items": [
{ "weight": 100, "value": 1 }
]
},
"expected": 0
},
{
"description": "five items (cannot be greedy by weight)",
"property": "maximumValue",
"input": {
"maximumWeight": 10,
"items": [
{ "weight": 2, "value": 5 },
{ "weight": 2, "value": 5 },
{ "weight": 2, "value": 5 },
{ "weight": 2, "value": 5 },
{ "weight": 10, "value": 21 }
]
},
"expected": 21
},
{
"description": "five items (cannot be greedy by value)",
"property": "maximumValue",
"input": {
"maximumWeight": 10,
"items": [
{ "weight": 2, "value": 20 },
{ "weight": 2, "value": 20 },
{ "weight": 2, "value": 20 },
{ "weight": 2, "value": 20 },
{ "weight": 10, "value": 50 }
]
},
"expected": 80
},
{
"description": "example knapsack",
"property": "maximumValue",
"input": {
"maximumWeight": 10,
"items": [
{ "weight": 5, "value": 10 },
{ "weight": 4, "value": 40 },
{ "weight": 6, "value": 30 },
{ "weight": 4, "value": 50 }
]
},
"expected": 90
},
{
"description": "8 items",
"property": "maximumValue",
"input": {
"maximumWeight": 104,
"items": [
{ "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 }
]
},
"expected": 900
},
{
"description": "15 items",
"property": "maximumValue",
"input": {
"maximumWeight": 750,
"items": [
{ "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 }
]
},
"expected": 1458
}
]
}
35 changes: 35 additions & 0 deletions exercises/knapsack/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 pairs, `wi` and `vi`, where the first element `wi` is the weight of
the *i*th item and `vi` is the value for that item.

For example:

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.
4 changes: 4 additions & 0 deletions exercises/knapsack/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
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"

0 comments on commit 3562b95

Please sign in to comment.