-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice_greedy_knapsack.py
45 lines (37 loc) · 1.26 KB
/
practice_greedy_knapsack.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
if __name__ == '__main__':
f = open('practice_greedy_knapsack_input.txt')
num_of_tests = int(f.readline())
for _ in range(num_of_tests):
n = int(f.readline())
# print(num_of_tests, n)
weight = {}
value = {}
vpw = {}
result = {}
line = f.readline().rstrip().split(" ")
for i in range(n):
weight[i] = int(line[i])
line = f.readline().rstrip().split(" ")
for i in range(n):
value[i] = int(line[i])
for i in range (n):
vpw[i] = value[i]/weight[i]
max_weight = int(f.readline())
print(vpw)
for i in sorted(vpw, reverse=False):
if (max_weight>0):
print("max_weight", max_weight)
print("weight[i]", weight[i])
print("value[i]", value[i])
# w = weight[i] % max_weight
if weight[i] > max_weight:
w = weight[i] - max_weight;
else:
w = weight[i]
print("w", w)
max_weight = max_weight - w
result[i] = w
print("max_weight", max_weight)
# print (vpw[i])
print(result)
# print(weight)