-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
82 lines (61 loc) · 2.15 KB
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
from collections import namedtuple
from itertools import combinations
import knapsack
def solve_it(input_data, language="rust"):
if language == "python":
return solve_it_python(input_data)
return solve_it_rust(input_data)
def solve_it_rust(input_data):
return knapsack.solve(input_data)
Item = namedtuple("Item", ["index", "value", "weight"])
def solve_it_python(input_data):
print("running in python", file=sys.stderr)
# parse the input
lines = input_data.split("\n")
firstLine = lines[0].split()
item_count = int(firstLine[0])
capacity = int(firstLine[1])
items = []
for i in range(1, item_count + 1):
line = lines[i]
parts = line.split()
items.append(Item(i - 1, int(parts[0]), int(parts[1])))
# a trivial algorithm for filling the knapsack
# it takes items in-order until the knapsack is full
value = 0
taken = [0] * len(items)
all_combinations = (
comb
for n in range(1, len(items) + 1)
for comb in combinations(items, n)
)
small_enough = (
comb
for comb in all_combinations
if sum(item.weight for item in comb) <= capacity
)
winner = max(small_enough, key=lambda items: sum(i.value for i in items))
value = sum(i.value for i in winner)
for idx, item in enumerate(items):
if item in winner:
taken[idx] = 1
# prepare the solution in the specified output format
output_data = str(value) + " " + str(1) + "\n"
output_data += " ".join(map(str, taken))
return output_data
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
file_location = sys.argv[1].strip()
with open(file_location, "r") as input_data_file:
input_data = input_data_file.read()
if len(sys.argv) > 2:
language = sys.argv[2].lower().strip()
print(solve_it(input_data, language=language))
else:
print(solve_it(input_data))
else:
print(
"This test requires an input file. Please select one from the data directory. (i.e. python solver.py ./data/ks_4_0)"
)