-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
6,034 additions
and
1 deletion.
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,22 @@ | ||
{ | ||
"authors": [ | ||
"kahgoh" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/Knapsack.re" | ||
], | ||
"test": [ | ||
"__tests__/Knapsack_test.re" | ||
], | ||
"example": [ | ||
".meta/src/Example.re" | ||
], | ||
"editor": [ | ||
"src/Item.re" | ||
] | ||
}, | ||
"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,47 @@ | ||
open Item; | ||
|
||
let rec last = (items: list('a)): 'a => { | ||
switch (items) { | ||
| [x] => x | ||
| [_next, ...rest] => last(rest) | ||
| _ => Js.Exn.raiseError("List can not be empty", "EmptyListError") | ||
}; | ||
}; | ||
|
||
let find_item_max_value = (item: item, last_values: list(int), capacity: int): int => { | ||
let value_without = List.nth(last_values, capacity); | ||
if (capacity >= item.weight) { | ||
let value_with = List.nth(last_values, (capacity - item.weight)) + item.value; | ||
max(value_with, value_without); | ||
} else { | ||
value_without | ||
} | ||
}; | ||
|
||
let rec calculate_max_values = (item: item, last_values: list(int), capacity: int, acc: list(int)) : list(int) => { | ||
switch (capacity) { | ||
| n when n < 0 => acc | ||
| _n => { | ||
let item_max_value = find_item_max_value(item, last_values, capacity) | ||
calculate_max_values(item, last_values, capacity - 1, [item_max_value, ...acc]) | ||
} | ||
}; | ||
}; | ||
|
||
let rec find_max_value = (items: list(item), last_values: list(int), capacity: int) : int => { | ||
switch (items) { | ||
| [] => last(last_values) | ||
| [next, ...rest] => { | ||
let next_values = calculate_max_values(next, last_values, capacity, []); | ||
find_max_value(rest, next_values, capacity) | ||
} | ||
}; | ||
}; | ||
|
||
let maximum_value = (items: list(item), capacity: int) : int => { | ||
let initial_values = List.init(capacity + 1, _ => 0); | ||
switch (items) { | ||
| [] => 0 | ||
| _ => find_max_value(items, initial_values, capacity) | ||
}; | ||
}; |
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,65 @@ | ||
open Item; | ||
open Jest; | ||
open Expect; | ||
open Knapsack; | ||
|
||
describe("knapsack", () => { | ||
test("no items", () => | ||
expect(maximum_value([], 100)) |> toEqual(0) | ||
); | ||
test("one item, too heavy", () => { | ||
expect(maximum_value([{weight: 100, value: 1}], 10)) |> toEqual(0) | ||
}); | ||
test("five items (cannot be greedy by weight)", () => { | ||
let items = [{weight: 2, value: 5}, | ||
{weight: 2, value: 5}, | ||
{weight: 2, value: 5}, | ||
{weight: 2, value: 5}, | ||
{weight: 10, value: 21}]; | ||
expect(maximum_value(items, 10)) |> toEqual(21) | ||
}); | ||
test("five items (cannot be greedy by value)", () => { | ||
let items = [{weight: 2, value: 20}, | ||
{weight: 2, value: 20}, | ||
{weight: 2, value: 20}, | ||
{weight: 2, value: 20}, | ||
{weight: 10, value: 50}]; | ||
expect(maximum_value(items, 10)) |> toEqual(80) | ||
}); | ||
test("example knapsack", () => { | ||
let items = [{weight: 5, value: 10}, | ||
{weight: 4, value: 40}, | ||
{weight: 6, value: 30}, | ||
{weight: 4, value: 50}]; | ||
expect(maximum_value(items, 10)) |> toEqual(90) | ||
}); | ||
test("8 items", () => { | ||
let 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}]; | ||
expect(maximum_value(items, 104)) |> toEqual(900) | ||
}); | ||
test("15 items", () => { | ||
let 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}]; | ||
expect(maximum_value(items, 750)) |> toEqual(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// This is the configuration file used by BuckleScript's build system bsb. Its documentation lives here: http://bucklescript.github.io/bucklescript/docson/#build-schema.json | ||
// BuckleScript comes with its own parser for bsconfig.json, which is normal JSON, with the extra support of comments and trailing commas. | ||
{ | ||
"name": "knapsack", | ||
"version": "0.1.0", | ||
"sources": [ | ||
{ | ||
"dir" : "src", | ||
"subdirs" : true | ||
}, | ||
{ | ||
"dir": "__tests__", | ||
"type": "dev" | ||
} | ||
], | ||
"package-specs": { | ||
"module": "commonjs", | ||
"in-source": true | ||
}, | ||
"suffix": ".bs.js", | ||
"bs-dependencies": [ | ||
// add your dependencies here. You'd usually install them normally through `npm install my-dependency`. If my-dependency has a bsconfig.json too, then everything will work seamlessly. | ||
], | ||
"bs-dev-dependencies": ["@glennsl/bs-jest"], | ||
"warnings": { | ||
"error" : "+101" | ||
}, | ||
"namespace": true, | ||
"refmt": 3 | ||
} |
Oops, something went wrong.