-
-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Knapsack exercise #768
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Item representation | ||
|
||
The items are represented by the `Item` struct in `Item.swift`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
7 changes: 7 additions & 0 deletions
7
exercises/practice/knapsack/.meta/Sources/Knapsack/Item.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Represents an item in knapsack. | ||
// There is be no need to edit this file and changes to this file are ignored by the Exercism test runners. | ||
|
||
struct Item { | ||
let weight: Int | ||
let value: Int | ||
} |
22 changes: 22 additions & 0 deletions
22
exercises/practice/knapsack/.meta/Sources/Knapsack/KnapsackExample.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
struct Knapsack { | ||
static func maximumValue(_ items: [Item], _ capacity: Int) -> Int { | ||
if items.count == 0 { | ||
return 0 | ||
} | ||
|
||
var values: Array<Int> = Array(repeating: 0, count: capacity + 1) | ||
for item in items { | ||
var next: Array<Int> = Array(repeating: 0, count: values.count) | ||
for i in 0..<values.count { | ||
if item.weight <= i { | ||
let valueWith = values[i - item.weight] + item.value | ||
next[i] = max(valueWith, values[i]) | ||
} else { | ||
next[i] = values[i] | ||
} | ||
} | ||
values = next | ||
} | ||
return values.last! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"authors": [ | ||
"kahgoh" | ||
], | ||
"files": { | ||
"solution": [ | ||
"Sources/Knapsack/Knapsack.swift" | ||
], | ||
"test": [ | ||
"Tests/KnapsackTests/KnapsackTests.swift" | ||
], | ||
"example": [ | ||
".meta/Sources/Knapsack/KnapsackExample.swift" | ||
], | ||
"editor": [ | ||
"Sources/Knapsack/Item.swift" | ||
] | ||
}, | ||
"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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import XCTest | ||
@testable import {{exercise|camelCase}} | ||
class {{exercise|camelCase}}Tests: XCTestCase { | ||
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false | ||
|
||
{% for case in cases %} | ||
{%- if forloop.first %} | ||
func test{{case.description |camelCase}}() { | ||
{%- else %} | ||
func test{{case.description |camelCase}}() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
{%- endif %} | ||
|
||
let items : [Item] = [ | ||
{%- for item in case.input.items %} | ||
{{item | knapsackItem}}, | ||
{%- endfor -%} | ||
] | ||
XCTAssertEqual(Knapsack.maximumValue(items, {{case.input.maximumWeight}}), {{case.expected}}) | ||
} | ||
{% endfor -%} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// swift-tools-version:5.3 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "Knapsack", | ||
products: [ | ||
.library( | ||
name: "Knapsack", | ||
targets: ["Knapsack"]) | ||
], | ||
dependencies: [], | ||
targets: [ | ||
.target( | ||
name: "Knapsack", | ||
dependencies: []), | ||
.testTarget( | ||
name: "KnapsackTests", | ||
dependencies: ["Knapsack"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Represents an item in knapsack. | ||
// There is be no need to edit this file and changes to this file are ignored by the Exercism test runners. | ||
|
||
struct Item { | ||
let weight: Int | ||
let value: Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
struct Knapsack { | ||
// Write your code for the 'Knapsack' exercise in this file. | ||
} |
99 changes: 99 additions & 0 deletions
99
exercises/practice/knapsack/Tests/KnapsackTests/KnapsackTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import XCTest | ||
|
||
@testable import Knapsack | ||
|
||
class KnapsackTests: XCTestCase { | ||
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false | ||
|
||
func testNoItems() { | ||
|
||
let items: [Item] = [] | ||
XCTAssertEqual(Knapsack.maximumValue(items, 100), 0) | ||
} | ||
|
||
func testOneItemTooHeavy() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
|
||
let items: [Item] = [ | ||
Item(weight: 100, value: 1) | ||
] | ||
XCTAssertEqual(Knapsack.maximumValue(items, 10), 0) | ||
} | ||
|
||
func testFiveItemsCannotBeGreedyByWeight() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
|
||
let items: [Item] = [ | ||
Item(weight: 2, value: 5), | ||
Item(weight: 2, value: 5), | ||
Item(weight: 2, value: 5), | ||
Item(weight: 2, value: 5), | ||
Item(weight: 10, value: 21), | ||
] | ||
XCTAssertEqual(Knapsack.maximumValue(items, 10), 21) | ||
} | ||
|
||
func testFiveItemsCannotBeGreedyByValue() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
|
||
let items: [Item] = [ | ||
Item(weight: 2, value: 20), | ||
Item(weight: 2, value: 20), | ||
Item(weight: 2, value: 20), | ||
Item(weight: 2, value: 20), | ||
Item(weight: 10, value: 50), | ||
] | ||
XCTAssertEqual(Knapsack.maximumValue(items, 10), 80) | ||
} | ||
|
||
func testExampleKnapsack() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
|
||
let items: [Item] = [ | ||
Item(weight: 5, value: 10), | ||
Item(weight: 4, value: 40), | ||
Item(weight: 6, value: 30), | ||
Item(weight: 4, value: 50), | ||
] | ||
XCTAssertEqual(Knapsack.maximumValue(items, 10), 90) | ||
} | ||
|
||
func test8Items() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
|
||
let items: [Item] = [ | ||
Item(weight: 25, value: 350), | ||
Item(weight: 35, value: 400), | ||
Item(weight: 45, value: 450), | ||
Item(weight: 5, value: 20), | ||
Item(weight: 25, value: 70), | ||
Item(weight: 3, value: 8), | ||
Item(weight: 2, value: 5), | ||
Item(weight: 2, value: 5), | ||
] | ||
XCTAssertEqual(Knapsack.maximumValue(items, 104), 900) | ||
} | ||
|
||
func test15Items() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
|
||
let items: [Item] = [ | ||
Item(weight: 70, value: 135), | ||
Item(weight: 73, value: 139), | ||
Item(weight: 77, value: 149), | ||
Item(weight: 80, value: 150), | ||
Item(weight: 82, value: 156), | ||
Item(weight: 87, value: 163), | ||
Item(weight: 90, value: 173), | ||
Item(weight: 94, value: 184), | ||
Item(weight: 98, value: 192), | ||
Item(weight: 106, value: 201), | ||
Item(weight: 110, value: 210), | ||
Item(weight: 113, value: 214), | ||
Item(weight: 115, value: 221), | ||
Item(weight: 118, value: 229), | ||
Item(weight: 120, value: 240), | ||
] | ||
XCTAssertEqual(Knapsack.maximumValue(items, 750), 1458) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indent should be 2 not 4 spaces. Otherwise lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just updated the exercise source files to use 2 spaces for indents. I wasn't sure about changing the indenting in the test template though. The test templates in the other exercises seem to use 4 spaces, although the formatter fixes the generated test to use 2 spaces. Let me know if the test template should also use 2 spaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not part of the Swift team but it sounds like the templates might benefit from an update. If they do get an update, that should probably be a different PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The track was a bit of a mess when I took over it tbh, the concepts used 2 indent while the practice exercises used 4 (the online editor used 4). Swift doesn't seem to agree on the indent size. The Swift book (written by apple) uses 4 indent, but if you look at libraries apple writes they use 2 (an example for reference here), I also belive the formatter by default uses 2. Thereby there could have been a transition error when I decided to transition the track to 2 indent. You are free to update the templates files but I don't see it as a necessity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or actually, Swift 6 includes a new testing framework so that means that we should likely transition to that at some point which means re-writting all test files so likely the easiest thing would be to just apply the fix then.