values = [ 4, 7, 10, 5, 3]
print(f"Max value of values: {max(values)}")
print(f"The index of the max value from values: {values.index(max(values))}")
Max value of values: 10
The index of the max value from values: 2
weights = [15, 8, 2, 6, 12]
print(f"Max value of weights: {max(weights)}")
print(f"The index of the max value from weights: {weights.index(max(weights))}")
Max value of weights: 15
The index of the max value from weights: 0
v = len(values)
for items in range(0,v):
item_v = max(values)
max_index = values.index(item_v)
item_w = weights[max_index]
values[max_index] = 0
print (f'Choose item number {items + 1} with index {max_index}, value = {item_v} and weight = {item_w}')
# set value to zero so we don't find it again
Choose item number 1 with index 2, value = 10 and weight = 2
Choose item number 2 with index 1, value = 7 and weight = 8
Choose item number 3 with index 3, value = 5 and weight = 6
Choose item number 4 with index 0, value = 4 and weight = 15
Choose item number 5 with index 4, value = 3 and weight = 12
values = [ 4, 7, 10, 5, 3] # we need to reload these lists because we modified them
weights = [15, 8, 2, 6, 12 ] # in loop above
current_w = 0
w = len(weights)
for item in range(0,w):
itemWeight = max(weights)
max_index = weights.index(itemWeight)
current_w = itemWeight + current_w
weights[max_index] = 0
print(current_w)
15
27
35
41
43