This notebook was prepared by Donne Martin. Source and license info is on GitHub.
total_weight = 8 items v | w 0 | 0 a 2 | 2 b 4 | 2 c 6 | 4 d 9 | 5 max value = 13 items v | w b 4 | 2 d 9 | 5
Refer to the Solution Notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start.
class Item(object):
def __init__(self, label, value, weight):
self.label = label
self.value = value
self.weight = weight
def __repr__(self):
return self.label + ' v:' + str(self.value) + ' w:' + str(self.weight)
class Knapsack(object):
def fill_knapsack(self, input_items, total_weight):
# TODO: Implement me
pass
The following unit test is expected to fail until you solve the challenge.
# %load test_knapsack.py
import unittest
class TestKnapsack(unittest.TestCase):
def test_knapsack_bottom_up(self):
knapsack = Knapsack()
self.assertRaises(TypeError, knapsack.fill_knapsack, None, None)
self.assertEqual(knapsack.fill_knapsack(0, 0), 0)
items = []
items.append(Item(label='a', value=2, weight=2))
items.append(Item(label='b', value=4, weight=2))
items.append(Item(label='c', value=6, weight=4))
items.append(Item(label='d', value=9, weight=5))
total_weight = 8
expected_value = 13
results = knapsack.fill_knapsack(items, total_weight)
self.assertEqual(results[0].label, 'd')
self.assertEqual(results[1].label, 'b')
total_value = 0
for item in results:
total_value += item.value
self.assertEqual(total_value, expected_value)
print('Success: test_knapsack_bottom_up')
def test_knapsack_top_down(self):
knapsack = KnapsackTopDown()
self.assertRaises(TypeError, knapsack.fill_knapsack, None, None)
self.assertEqual(knapsack.fill_knapsack(0, 0), 0)
items = []
items.append(Item(label='a', value=2, weight=2))
items.append(Item(label='b', value=4, weight=2))
items.append(Item(label='c', value=6, weight=4))
items.append(Item(label='d', value=9, weight=5))
total_weight = 8
expected_value = 13
self.assertEqual(knapsack.fill_knapsack(items, total_weight), expected_value)
print('Success: test_knapsack_top_down')
def main():
test = TestKnapsack()
test.test_knapsack_bottom_up()
test.test_knapsack_top_down()
if __name__ == '__main__':
main()
Review the Solution Notebook for a discussion on algorithms and code solutions.