Skip to content
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

Knapsack : sync expected test results and input data with problem-specifications. #1799

Merged
merged 3 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions exercises/knapsack/example.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
def solve_knapsack(max_weight, items):
t = [[0 for _ in range(len(items) + 1)] for _ in range(max_weight + 1)]
for weight in range(1, max_weight + 1):
for i, item in enumerate(items, 1):
def maximum_value(maximum_weight, items):
totals = [[0 for _ in range(len(items) + 1)]
for _ in range(maximum_weight + 1)]

for weight in range(1, maximum_weight + 1):
for index, item in enumerate(items, 1):
if item["weight"] <= weight:
value = item["value"] + t[weight - item["weight"]][i - 1]
value_without_item = t[weight][i - 1]
t[weight][i] = max(value, value_without_item)
value = item["value"] + \
totals[weight - item["weight"]][index - 1]

value_without_item = totals[weight][index - 1]
totals[weight][index] = max(value, value_without_item)
else:
t[weight][i] = t[weight][i - 1]
return t[max_weight][len(items)]
totals[weight][index] = totals[weight][index - 1]
return totals[maximum_weight][len(items)]
2 changes: 1 addition & 1 deletion exercises/knapsack/knapsack.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def solve_knapsack(max_weight, items):
def maximum_value(maximum_weight, items):
pass
83 changes: 41 additions & 42 deletions exercises/knapsack/knapsack_test.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
import unittest

from knapsack import solve_knapsack
from knapsack import maximum_value


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0

class ChangeTest(unittest.TestCase):
def test_no_items(self):
self.assertEqual(solve_knapsack(100, []), 0)
self.assertEqual(maximum_value(100, []), 0)

def test_one_item_too_heavy(self):
self.assertEqual(solve_knapsack(10, [{"weight": 100, "value": 1}]), 0)
self.assertEqual(maximum_value(10, [{"weight": 100, "value": 1}]), 0)

def test_cannot_be_greedy_by_weight(self):
self.assertEqual(solve_knapsack(10,
[{"weight": 2, "value": 5},
{"weight": 2, "value": 5},
{"weight": 2, "value": 5},
{"weight": 2, "value": 5},
{"weight": 10, "value": 21}]), 21)
self.assertEqual(maximum_value(10, [{"weight": 2, "value": 5},
{"weight": 2, "value": 5},
{"weight": 2, "value": 5},
{"weight": 2, "value": 5},
{"weight": 10, "value": 21}]), 21)

def test_cannot_be_greedy_by_value(self):
self.assertEqual(solve_knapsack(10, [{"weight": 2, "value": 20},
{"weight": 2, "value": 20},
{"weight": 2, "value": 20},
{"weight": 2, "value": 20},
{"weight": 10, "value": 50}]), 80)
self.assertEqual(maximum_value(10, [{"weight": 2, "value": 20},
{"weight": 2, "value": 20},
{"weight": 2, "value": 20},
{"weight": 2, "value": 20},
{"weight": 10, "value": 50}]), 80)

def test_example_knapsack(self):
self.assertEqual(solve_knapsack(10, [{"weight": 5, "value": 10},
{"weight": 4, "value": 40},
{"weight": 6, "value": 30},
{"weight": 4, "value": 50}]), 90)
self.assertEqual(maximum_value(10, [{"weight": 5, "value": 10},
{"weight": 4, "value": 40},
{"weight": 6, "value": 30},
{"weight": 4, "value": 50}]), 90)

def test_eight_items(self):
self.assertEqual(solve_knapsack(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}]), 900)
self.assertEqual(maximum_value(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}]), 900)

def test_fifteen_items(self):
self.assertEqual(solve_knapsack(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}]), 1458)
self.assertEqual(maximum_value(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}]), 1458)


if __name__ == "__main__":
Expand Down