-
-
Notifications
You must be signed in to change notification settings - Fork 139
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
1 parent
1144087
commit 10f0cfe
Showing
7 changed files
with
283 additions
and
0 deletions.
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,35 @@ | ||
# Instructions | ||
|
||
In this exercise, let's try to solve a classic problem. | ||
|
||
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. | ||
|
||
For example: | ||
|
||
```none | ||
Items: [ | ||
{ "weight": 5, "value": 10 }, | ||
{ "weight": 4, "value": 40 }, | ||
{ "weight": 6, "value": 30 }, | ||
{ "weight": 4, "value": 50 } | ||
] | ||
Knapsack Limit: 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,19 @@ | ||
{ | ||
"authors": [ | ||
"tomasnorre" | ||
], | ||
"files": { | ||
"solution": [ | ||
"Knapsack.php" | ||
], | ||
"test": [ | ||
"KnapsackTest.php" | ||
], | ||
"example": [ | ||
".meta/example.php" | ||
] | ||
}, | ||
"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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
class Knapsack | ||
{ | ||
public function getMaximumValue(int $maximumWeight, array $items): int | ||
{ | ||
$table = array_fill(0, count($items) + 1, array_fill(0, $maximumWeight + 1, 0)); | ||
|
||
for ($i = 0, $iMax = count($items); $i < $iMax; $i++) { | ||
for ($capacity = 1; $capacity < $maximumWeight + 1; $capacity++) { | ||
$value = $items[$i]['value']; | ||
$weight = $items[$i]['weight']; | ||
if ($weight > $capacity) { | ||
$table[$i + 1][$capacity] = $table[$i][$capacity]; | ||
} else { | ||
$table[$i + 1][$capacity] = max( | ||
$table[$i][$capacity], | ||
$value + $table[$i][$capacity - $weight] | ||
); | ||
} | ||
} | ||
} | ||
|
||
return $table[count($items)][$maximumWeight]; | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
/* | ||
* By adding type hints and enabling strict type checking, code can become | ||
* easier to read, self-documenting and reduce the number of potential bugs. | ||
* By default, type declarations are non-strict, which means they will attempt | ||
* to change the original type to match the type specified by the | ||
* type-declaration. | ||
* | ||
* In other words, if you pass a string to a function requiring a float, | ||
* it will attempt to convert the string value to a float. | ||
* | ||
* To enable strict mode, a single declare directive must be placed at the top | ||
* of the file. | ||
* This means that the strictness of typing is configured on a per-file basis. | ||
* This directive not only affects the type declarations of parameters, but also | ||
* a function's return type. | ||
* | ||
* For more info review the Concept on strict type checking in the PHP track | ||
* <link>. | ||
* | ||
* To disable strict typing, comment out the directive below. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
class Knapsack | ||
{ | ||
public function getMaximumValue(int $maximumWeight, array $items): int | ||
{ | ||
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); | ||
} | ||
} |
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,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class KnapsackTest extends TestCase | ||
{ | ||
private Knapsack $knapsack; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
require_once 'Knapsack.php'; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->knapsack = new Knapsack(); | ||
} | ||
|
||
/** | ||
* uuid: 3993a824-c20e-493d-b3c9-ee8a7753ee59 | ||
*/ | ||
public function testNoItems(): void | ||
{ | ||
$this->assertEquals(0, $this->knapsack->getMaximumValue(100, [])); | ||
} | ||
|
||
/** | ||
* uuid: 1d39e98c-6249-4a8b-912f-87cb12e506b0 | ||
*/ | ||
public function testOneItemTooHeavy(): void | ||
{ | ||
$items = [[ 'weight' => 100, 'value' => 1 ]]; | ||
$this->assertEquals(0, $this->knapsack->getMaximumValue(10, $items)); | ||
} | ||
|
||
/** | ||
* uuid: 833ea310-6323-44f2-9d27-a278740ffbd8 | ||
*/ | ||
public function testFiveItemsCannotBeGreedyByWeight(): void | ||
{ | ||
$items = [ | ||
[ 'weight' => 2, 'value' => 5 ], | ||
[ 'weight' => 2, 'value' => 5 ], | ||
[ 'weight' => 2, 'value' => 5 ], | ||
[ 'weight' => 2, 'value' => 5 ], | ||
[ 'weight' => 10, 'value' => 21 ], | ||
]; | ||
$this->assertEquals(21, $this->knapsack->getMaximumValue(10, $items)); | ||
} | ||
|
||
/** | ||
* uuid: 277cdc52-f835-4c7d-872b-bff17bab2456 | ||
*/ | ||
public function testFiveItemsCannotBeGreedyByValue(): void | ||
{ | ||
$items = [ | ||
[ 'weight' => 2, 'value' => 20 ], | ||
[ 'weight' => 2, 'value' => 20 ], | ||
[ 'weight' => 2, 'value' => 20 ], | ||
[ 'weight' => 2, 'value' => 20 ], | ||
[ 'weight' => 10, 'value' => 50 ], | ||
]; | ||
$this->assertEquals(80, $this->knapsack->getMaximumValue(10, $items)); | ||
} | ||
|
||
/** | ||
* uuid: 81d8e679-442b-4f7a-8a59-7278083916c9 | ||
*/ | ||
public function testExampleKnapsack(): void | ||
{ | ||
$items = [ | ||
[ 'weight' => 5, 'value' => 10 ], | ||
[ 'weight' => 4, 'value' => 40 ], | ||
[ 'weight' => 6, 'value' => 30 ], | ||
[ 'weight' => 4, 'value' => 50 ], | ||
]; | ||
$this->assertEquals(90, $this->knapsack->getMaximumValue(10, $items)); | ||
} | ||
|
||
/** | ||
* uuid: f23a2449-d67c-4c26-bf3e-cde020f27ecc | ||
*/ | ||
public function testEightItems(): void | ||
{ | ||
$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 ], | ||
]; | ||
$this->assertEquals(900, $this->knapsack->getMaximumValue(104, $items)); | ||
} | ||
|
||
/** | ||
* uuid: 7c682ae9-c385-4241-a197-d2fa02c81a11 | ||
*/ | ||
public function testFifteenItems(): void | ||
{ | ||
$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 ], | ||
]; | ||
$this->assertEquals(1458, $this->knapsack->getMaximumValue(750, $items)); | ||
} | ||
} |