From 0f4125db53f51939cd2a44230c7a437ade7bca04 Mon Sep 17 00:00:00 2001 From: zaibod Date: Thu, 18 Jul 2024 01:00:11 +0200 Subject: [PATCH] implemented python knapsack solver --- python/knapsack/knapsack.py | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/python/knapsack/knapsack.py b/python/knapsack/knapsack.py index d9057a11..e1863aa8 100644 --- a/python/knapsack/knapsack.py +++ b/python/knapsack/knapsack.py @@ -3,4 +3,43 @@ import sys from knapsack01 import HSKnapsack +if len(sys.argv) != 3: + raise TypeError('This script expects exactly 2 arguments. Input file (argument 1) and output file (argument 2).') +input_path = sys.argv[1] +output_path = sys.argv[2] + +with open(input_path, 'r') as input_file: + lines = input_file.readlines() + +# first line gives number of items +number_items : int = int(lines[0]) + +profits: list[int] = [] +weights: list[int] = [] + +# read items into profits, weights lists +for i in range(number_items): + item = lines[i + 1].split(' ') + profits.append(int(item[1])) + weights.append(int(item[2])) + +# last line contains maximum capacity of the knapsack +capacity: int = int(lines[-1]) + +# let the library solve the problem ^^ +knapsack = HSKnapsack(capacity, profits, weights) +max_profit, max_solution = knapsack.maximize() + +# max_solution only contains indicator whether item is present, collect items for solutions in a list +items_in_solution: list[list[int]] = [] +for i in range(number_items): + if max_solution[i] == 1: + items_in_solution.append([profits[i], weights[i]]) + +# returning the solution as a single line with the maximum profit +# followed by the items present represented by profit/weight +with open(output_path, 'w') as output_file: + output_file.write(str(max_profit) + '\n') + for item in items_in_solution: + output_file.write(f"{item[0]} {item[1]}\n")