mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-19 10:48:58 +08:00
Add a naive recursive implementation of 0-1 Knapsack Problem (#2743)
* Add naive recursive implementation of 0-1 Knapsack problem * Fix shadowing * Add doctest * Fix type hints * Add link to wiki * Blacked the file * Fix isort * Move knapsack / add readme and more tests * Add missed main in tests
This commit is contained in:
52
knapsack/test_knapsack.py
Normal file
52
knapsack/test_knapsack.py
Normal file
@ -0,0 +1,52 @@
|
||||
"""
|
||||
Created on Fri Oct 16 09:31:07 2020
|
||||
|
||||
@author: Dr. Tobias Schröder
|
||||
@license: MIT-license
|
||||
|
||||
This file contains the test-suite for the knapsack problem.
|
||||
"""
|
||||
import unittest
|
||||
|
||||
from knapsack import knapsack as k
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
def test_base_case(self):
|
||||
"""
|
||||
test for the base case
|
||||
"""
|
||||
cap = 0
|
||||
val = [0]
|
||||
w = [0]
|
||||
c = len(val)
|
||||
self.assertEqual(k.knapsack(cap, w, val, c), 0)
|
||||
|
||||
val = [60]
|
||||
w = [10]
|
||||
c = len(val)
|
||||
self.assertEqual(k.knapsack(cap, w, val, c), 0)
|
||||
|
||||
def test_easy_case(self):
|
||||
"""
|
||||
test for the base case
|
||||
"""
|
||||
cap = 3
|
||||
val = [1, 2, 3]
|
||||
w = [3, 2, 1]
|
||||
c = len(val)
|
||||
self.assertEqual(k.knapsack(cap, w, val, c), 5)
|
||||
|
||||
def test_knapsack(self):
|
||||
"""
|
||||
test for the knapsack
|
||||
"""
|
||||
cap = 50
|
||||
val = [60, 100, 120]
|
||||
w = [10, 20, 30]
|
||||
c = len(val)
|
||||
self.assertEqual(k.knapsack(cap, w, val, c), 220)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Reference in New Issue
Block a user