From 034c04fbf093fdb7c94e8acbe1544d7618788b34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Santos?= Date: Sat, 13 Jul 2024 16:17:19 +0100 Subject: [PATCH] Prepare exercise for release --- config.json | 21 +++++++++++-------- .../practice/knapsack/.docs/instructions.md | 20 +++++------------- .../practice/knapsack/.docs/introduction.md | 8 +++++++ exercises/practice/knapsack/.meta/example.go | 4 ++-- exercises/practice/knapsack/.meta/gen.go | 4 ++-- exercises/practice/knapsack/cases_test.go | 16 +++++++------- exercises/practice/knapsack/knapsack.go | 9 ++++---- exercises/practice/knapsack/knapsack_test.go | 10 ++++++++- 8 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 exercises/practice/knapsack/.docs/introduction.md diff --git a/config.json b/config.json index 158130f13..abea8d081 100644 --- a/config.json +++ b/config.json @@ -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", @@ -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", diff --git a/exercises/practice/knapsack/.docs/instructions.md b/exercises/practice/knapsack/.docs/instructions.md index fadcee1b1..3411db988 100644 --- a/exercises/practice/knapsack/.docs/instructions.md +++ b/exercises/practice/knapsack/.docs/instructions.md @@ -1,24 +1,15 @@ # 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 }, @@ -26,10 +17,9 @@ Items: [ { "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. diff --git a/exercises/practice/knapsack/.docs/introduction.md b/exercises/practice/knapsack/.docs/introduction.md new file mode 100644 index 000000000..9b2bed8b4 --- /dev/null +++ b/exercises/practice/knapsack/.docs/introduction.md @@ -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. diff --git a/exercises/practice/knapsack/.meta/example.go b/exercises/practice/knapsack/.meta/example.go index 35db4824f..dcc16874e 100644 --- a/exercises/practice/knapsack/.meta/example.go +++ b/exercises/practice/knapsack/.meta/example.go @@ -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) diff --git a/exercises/practice/knapsack/.meta/gen.go b/exercises/practice/knapsack/.meta/gen.go index 2fe8081b9..bb746e1bb 100644 --- a/exercises/practice/knapsack/.meta/gen.go +++ b/exercises/practice/knapsack/.meta/gen.go @@ -41,7 +41,7 @@ var tmpl = `package knapsack type maximumValueCaseInput struct { MaximumWeight int - Items []item + Items []Item } type maximumValueTest struct { @@ -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}}, diff --git a/exercises/practice/knapsack/cases_test.go b/exercises/practice/knapsack/cases_test.go index a4b559be5..ee6fc80ad 100644 --- a/exercises/practice/knapsack/cases_test.go +++ b/exercises/practice/knapsack/cases_test.go @@ -7,7 +7,7 @@ package knapsack type maximumValueCaseInput struct { MaximumWeight int - Items []item + Items []Item } type maximumValueTest struct { @@ -22,7 +22,7 @@ var maximumValueTests = []maximumValueTest{ description: "no items", input: maximumValueCaseInput{ MaximumWeight: 100, - Items: []item{}, + Items: []Item{}, }, expected: 0, }, @@ -31,7 +31,7 @@ var maximumValueTests = []maximumValueTest{ description: "one item, too heavy", input: maximumValueCaseInput{ MaximumWeight: 10, - Items: []item{ + Items: []Item{ { Weight: 100, @@ -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, @@ -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, @@ -116,7 +116,7 @@ var maximumValueTests = []maximumValueTest{ description: "example knapsack", input: maximumValueCaseInput{ MaximumWeight: 10, - Items: []item{ + Items: []Item{ { Weight: 5, @@ -146,7 +146,7 @@ var maximumValueTests = []maximumValueTest{ description: "8 items", input: maximumValueCaseInput{ MaximumWeight: 104, - Items: []item{ + Items: []Item{ { Weight: 25, @@ -196,7 +196,7 @@ var maximumValueTests = []maximumValueTest{ description: "15 items", input: maximumValueCaseInput{ MaximumWeight: 750, - Items: []item{ + Items: []Item{ { Weight: 70, diff --git a/exercises/practice/knapsack/knapsack.go b/exercises/practice/knapsack/knapsack.go index 92ee26777..dc5dabb76 100644 --- a/exercises/practice/knapsack/knapsack.go +++ b/exercises/practice/knapsack/knapsack.go @@ -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") } diff --git a/exercises/practice/knapsack/knapsack_test.go b/exercises/practice/knapsack/knapsack_test.go index 3a7271714..ca1a47744 100644 --- a/exercises/practice/knapsack/knapsack_test.go +++ b/exercises/practice/knapsack/knapsack_test.go @@ -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) } } }