-
-
Notifications
You must be signed in to change notification settings - Fork 110
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 #700
Merged
Merged
Add Knapsack #700
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,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. |
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,19 @@ | ||
{ | ||
"authors": [ | ||
"kahgoh" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/Knapsack.elm" | ||
], | ||
"test": [ | ||
"tests/Tests.elm" | ||
], | ||
"example": [ | ||
".meta/src/Knapsack.example.elm" | ||
] | ||
}, | ||
"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" | ||
} |
63 changes: 63 additions & 0 deletions
63
exercises/practice/knapsack/.meta/src/Knapsack.example.elm
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,63 @@ | ||
module Knapsack exposing (Item, maximumValue) | ||
|
||
|
||
type alias Item = | ||
{ value : Int | ||
, weight : Int | ||
} | ||
|
||
|
||
maximumValue : Int -> List Item -> Int | ||
maximumValue capacity items = | ||
doMaximumValue items (List.repeat (capacity + 1) 0) | ||
|
||
|
||
last : List Int -> Int | ||
last list = | ||
List.foldl (\x _ -> x) 0 list | ||
|
||
|
||
valueAt : List a -> Int -> Maybe a | ||
valueAt list index = | ||
case list of | ||
[] -> | ||
Nothing | ||
|
||
next :: remaining -> | ||
if index <= 0 then | ||
Just next | ||
|
||
else | ||
valueAt remaining (index - 1) | ||
|
||
|
||
nextRow : Item -> List Int -> List Int | ||
nextRow item lastRow = | ||
List.indexedMap | ||
(\availableCapacity value -> | ||
let | ||
requiredCapacity = | ||
availableCapacity - item.weight | ||
in | ||
if requiredCapacity < 0 then | ||
value | ||
|
||
else | ||
case valueAt lastRow requiredCapacity of | ||
Just valueBeforeItem -> | ||
max (valueBeforeItem + item.value) value | ||
|
||
Nothing -> | ||
value | ||
) | ||
lastRow | ||
|
||
|
||
doMaximumValue : List Item -> List Int -> Int | ||
doMaximumValue items lastRow = | ||
case items of | ||
[] -> | ||
last lastRow | ||
|
||
next :: rest -> | ||
doMaximumValue rest (nextRow next lastRow) |
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,29 @@ | ||
{ | ||
"type": "application", | ||
"source-directories": [ | ||
"src" | ||
], | ||
"elm-version": "0.19.1", | ||
"dependencies": { | ||
"direct": { | ||
"elm/core": "1.0.5", | ||
"elm/json": "1.1.3", | ||
"elm/parser": "1.1.0", | ||
"elm/random": "1.0.0", | ||
"elm/regex": "1.0.0", | ||
"elm/time": "1.0.0" | ||
}, | ||
"indirect": {} | ||
}, | ||
"test-dependencies": { | ||
"direct": { | ||
"elm-explorations/test": "2.1.0", | ||
"rtfeldman/elm-iso8601-date-strings": "1.1.4" | ||
}, | ||
"indirect": { | ||
"elm/bytes": "1.0.8", | ||
"elm/html": "1.0.0", | ||
"elm/virtual-dom": "1.0.3" | ||
} | ||
} | ||
} |
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,12 @@ | ||
module Knapsack exposing (Item, maximumValue) | ||
|
||
|
||
type alias Item = | ||
{ value : Int | ||
, weight : Int | ||
} | ||
|
||
|
||
maximumValue : Int -> List Item -> Int | ||
maximumValue capacity items = | ||
Debug.todo "Please implement maximumValue" |
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,85 @@ | ||
module Tests exposing (tests) | ||
|
||
import Expect | ||
import Knapsack exposing (..) | ||
import Test exposing (..) | ||
|
||
|
||
tests : Test | ||
tests = | ||
describe "Knapsack" | ||
[ test "no items" <| | ||
\() -> | ||
maximumValue 100 [] |> Expect.equal 0 | ||
, skip <| | ||
test "one item, too heavy" <| | ||
\() -> | ||
maximumValue 10 [ { weight = 100, value = 1 } ] |> Expect.equal 0 | ||
, skip <| | ||
test "five items (cannot be greedy by weight)" <| | ||
\() -> | ||
maximumValue 10 | ||
[ { weight = 2, value = 5 } | ||
, { weight = 2, value = 5 } | ||
, { weight = 2, value = 5 } | ||
, { weight = 2, value = 5 } | ||
, { weight = 10, value = 21 } | ||
] | ||
|> Expect.equal 21 | ||
, skip <| | ||
test "five items (cannot be greedy by value)" <| | ||
\() -> | ||
maximumValue 10 | ||
[ { weight = 2, value = 20 } | ||
, { weight = 2, value = 20 } | ||
, { weight = 2, value = 20 } | ||
, { weight = 2, value = 20 } | ||
, { weight = 10, value = 50 } | ||
] | ||
|> Expect.equal 80 | ||
, skip <| | ||
test "example knapsack" <| | ||
\() -> | ||
maximumValue 10 | ||
[ { weight = 5, value = 10 } | ||
, { weight = 4, value = 40 } | ||
, { weight = 6, value = 30 } | ||
, { weight = 4, value = 50 } | ||
] | ||
|> Expect.equal 90 | ||
, skip <| | ||
test "8 items" <| | ||
\() -> | ||
maximumValue 104 | ||
[ { 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 } | ||
] | ||
|> Expect.equal 900 | ||
, skip <| | ||
test "15 items" <| | ||
\() -> | ||
maximumValue 750 | ||
[ { 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 } | ||
] | ||
|> Expect.equal 1458 | ||
] |
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.
[You don't need to do anything here] This is also a good candidate for #692