Skip to content

Commit

Permalink
Prepare exercise for release
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerfcsantos committed Jul 13, 2024
1 parent 835f7b2 commit 034c04f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 41 deletions.
21 changes: 12 additions & 9 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1804,15 +1804,6 @@
"structs"
]
},
{
"slug": "knapsack",
"name": "Knapsack",
"uuid": "eae2fd23-c484-4dc6-b8fe-1fa055bab8ba",
"practices": [],
"prerequisites": [],
"difficulty": 5,
"topics": []
},
{
"slug": "state-of-tic-tac-toe",
"name": "State of Tic-Tac-Toe",
Expand All @@ -1821,6 +1812,18 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "knapsack",
"name": "Knapsack",
"uuid": "eae2fd23-c484-4dc6-b8fe-1fa055bab8ba",
"practices": [],
"prerequisites": [],
"difficulty": 6,
"topics": [
"algorithms",
"dynamic_programming"
]
},
{
"slug": "dominoes",
"name": "Dominoes",
Expand Down
20 changes: 5 additions & 15 deletions exercises/practice/knapsack/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
# Instructions

In this exercise, let's try to solve a classic problem.
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.

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.
All values given will be strictly positive.
Bob can take only one of each item.

For example:

```none
```text
Items: [
{ "weight": 5, "value": 10 },
{ "weight": 4, "value": 40 },
{ "weight": 6, "value": 30 },
{ "weight": 4, "value": 50 }
]
Knapsack Limit: 10
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.
4 changes: 2 additions & 2 deletions exercises/practice/knapsack/.meta/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package knapsack

import "math"

type item struct {
type Item struct {
Weight, Value int
}

func Knapsack(maximumWeight int, items []item) int {
func Knapsack(maximumWeight int, items []Item) int {
amountOfItems := len(items)
knapsack := make([][]int, amountOfItems+1)

Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/knapsack/.meta/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var tmpl = `package knapsack
type maximumValueCaseInput struct {
MaximumWeight int
Items []item
Items []Item
}
type maximumValueTest struct {
Expand All @@ -56,7 +56,7 @@ var maximumValueTests = []maximumValueTest {
description: {{printf "%q" .Description}},
input: maximumValueCaseInput {
MaximumWeight: {{printf "%d" .Input.MaximumWeight}},
Items: []item {
Items: []Item {
{{range .Input.Items}}
{
Weight: {{printf "%d" .Weight}},
Expand Down
16 changes: 8 additions & 8 deletions exercises/practice/knapsack/cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package knapsack

type maximumValueCaseInput struct {
MaximumWeight int
Items []item
Items []Item
}

type maximumValueTest struct {
Expand All @@ -22,7 +22,7 @@ var maximumValueTests = []maximumValueTest{
description: "no items",
input: maximumValueCaseInput{
MaximumWeight: 100,
Items: []item{},
Items: []Item{},
},
expected: 0,
},
Expand All @@ -31,7 +31,7 @@ var maximumValueTests = []maximumValueTest{
description: "one item, too heavy",
input: maximumValueCaseInput{
MaximumWeight: 10,
Items: []item{
Items: []Item{

{
Weight: 100,
Expand All @@ -46,7 +46,7 @@ var maximumValueTests = []maximumValueTest{
description: "five items (cannot be greedy by weight)",
input: maximumValueCaseInput{
MaximumWeight: 10,
Items: []item{
Items: []Item{

{
Weight: 2,
Expand Down Expand Up @@ -81,7 +81,7 @@ var maximumValueTests = []maximumValueTest{
description: "five items (cannot be greedy by value)",
input: maximumValueCaseInput{
MaximumWeight: 10,
Items: []item{
Items: []Item{

{
Weight: 2,
Expand Down Expand Up @@ -116,7 +116,7 @@ var maximumValueTests = []maximumValueTest{
description: "example knapsack",
input: maximumValueCaseInput{
MaximumWeight: 10,
Items: []item{
Items: []Item{

{
Weight: 5,
Expand Down Expand Up @@ -146,7 +146,7 @@ var maximumValueTests = []maximumValueTest{
description: "8 items",
input: maximumValueCaseInput{
MaximumWeight: 104,
Items: []item{
Items: []Item{

{
Weight: 25,
Expand Down Expand Up @@ -196,7 +196,7 @@ var maximumValueTests = []maximumValueTest{
description: "15 items",
input: maximumValueCaseInput{
MaximumWeight: 750,
Items: []item{
Items: []Item{

{
Weight: 70,
Expand Down
9 changes: 5 additions & 4 deletions exercises/practice/knapsack/knapsack.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package knapsack

type item struct {
type Item struct {
Weight, Value int
}

// Knapsack takes in a maximum carrying capacity and a collection of items, it should return the best possible value
// of items while remaining within the carrying capacity
func Knapsack(maximumWeight int, items []item) int {
// Knapsack takes in a maximum carrying capacity and a collection of items
// and returns the maximum value that can be carried by the knapsack
// given that the knapsack can only carry a maximum weight given by maximumWeight
func Knapsack(maximumWeight int, items []Item) int {
panic("Please implement the Knapsack() function")
}
10 changes: 9 additions & 1 deletion exercises/practice/knapsack/knapsack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ func TestKnapsack(t *testing.T) {
expected := tc.expected

if actual != expected {
t.Fatalf("Knapsack(%+v) = %d, want %d", tc.input, actual, tc.expected)
t.Fatalf("Knapsack(%d, %+v) = %d, want %d", tc.input.MaximumWeight, tc.input.Items, actual, tc.expected)
}
}
}

func BenchmarkKnapsack(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range maximumValueTests {
Knapsack(tc.input.MaximumWeight, tc.input.Items)
}
}
}

0 comments on commit 034c04f

Please sign in to comment.