From 1a33710954a74eed35b5ef845d029fdf203ec8f9 Mon Sep 17 00:00:00 2001 From: Maksym Kurets Date: Fri, 4 Dec 2026 22:29:26 +0100 Subject: [PATCH] u11 update --- u11/knapsack.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/u11/knapsack.py b/u11/knapsack.py index 6a58dca..43878e9 100644 --- a/u11/knapsack.py +++ b/u11/knapsack.py @@ -3,7 +3,7 @@ def maximum_value(maximum_weight, items): return 0 stav = {0: 0} - + for item in items: w = item["weight"] v = item["value"] @@ -14,18 +14,19 @@ def maximum_value(maximum_weight, items): if NEwW <= maximum_weight: NewV = currV + v if NewV > Novystav.get(NEwW, 0): - Novystav[NEwW] = NewV + Novystav[NEwW] = NewV stav = Novystav return max(stav.values()) - + + def maximum_value_with_items(maximum_weight, items): if not items or maximum_weight <= 0: return 0, [] - - stav = {0: (0,[])} - + + stav = {0: (0, [])} + for item in items: w = item["weight"] v = item["value"] @@ -38,17 +39,13 @@ def maximum_value_with_items(maximum_weight, items): if NewV > Novystav.get(NEwW, (0, []))[0]: Novystav[NEwW] = (NewV, vibir + [item]) - stav = Novystav - + stav = Novystav + BV = 0 Bvibir = [] for v, vibir in stav.values(): - if v >BV: - BV = v - Bvibir = vibir + if v > BV: + BV = v + Bvibir = vibir return BV, Bvibir - - - -