def testBorderCases(self): n = random.randint(1, 10 ^ 3) print(f'n: {n}') capacity = random.randint(0, 2 * (10 ^ 6)) print(f'capacity: {capacity}') weights = list() values = list() for i in range(n): weight = random.randint(0, 2 * (10 ^ 6)) value = random.randint(1, 2 * (10 ^ 6)) weights.append(weight) values.append(value) opt_value = get_optimal_value(capacity, values, weights) print("{:.4f}".format(opt_value)) self.testBorderCases()
def test_with_capacity_and_three_items_of_larger_total_weight(self): self.assertAlmostEqual(180.0000, get_optimal_value(50, [20, 50, 30], [60, 100, 120]), places=4)
def test_with_capacity_and_two_items_of_lesser_total_weight(self): self.assertAlmostEqual(160.0000, get_optimal_value(50, [10, 20], [60, 100]), places=4)
def test_with_capacity_and_one_item_of_larger_total_weight(self): self.assertAlmostEqual(166.6667, get_optimal_value(10, [30], [500]), places=3)
def test_with_zero_capacity_and_weights_and_values(self): self.assertEqual(0., get_optimal_value(0, [1], [10])) self.assertEqual(0., get_optimal_value(0, [1, 2, 3], [10, 20, 30]))
def test_with_zero_capacity_and_empty_weights_and_empty_values(self): self.assertEqual(0., get_optimal_value(0, [], []))
def test_with_unbalanced_weights_and_values(self): get_optimal_value(0, [1], [1, 2, 3])
def test_get_optimal_value_2(): assert (int(get_optimal_value(10,[500],[30])) == 166)
def test_with_preceeding_lower_bound_among_values(self): get_optimal_value(0, [], [1, -1, 3])
def test_with_preceeding_lower_bound_value(self): get_optimal_value(0, [], [-1])
Input: 3 50 60 20 100 50 120 30 Output: 180.0000 To achieve the value 180, we take the first item and the third item into the bag. Sample 2. Input: 1 10 500 30 Output: 166.6667 Here, we just take one third of the only available item. """ from fractional_knapsack import get_optimal_value from test.asserts import assert_equal assert_equal( "0.0000", "{:.4f}".format(get_optimal_value(0, [20, 50, 30], [60, 100, 120])), "zero capacity") assert_equal( "180.0000", "{:.4f}".format(get_optimal_value(50, [20, 50, 30], [60, 100, 120])), "sample 1") assert_equal("166.6667", "{:.4f}".format(get_optimal_value(10, [30], [500])), "sample 2")
from fractional_knapsack import get_optimal_value assert get_optimal_value(capacity=50, weights=[20, 50, 30], values=[60, 100, 120]) == 180 assert get_optimal_value(capacity=10, weights=[30], values=[100]) == 166.667
def test_get_optimal_value_3(): assert (int(get_optimal_value(1000,[500],[30])) == 500)
def test_with_preceeding_lower_bound_capacity(self): get_optimal_value(-1, [], [])
def test_with_non_empty_weights_and_empty_values(self): get_optimal_value(0, [1], [])
def test_get_optimal_value(capacity, weights, values, expected): assert get_optimal_value(capacity, weights, values) == expected
def test_get_optimal_value_1(): assert (get_optimal_value(50,[60,100,120],[20,50,30]) == 180)