values = [ 4, 7, 10, 5, 3]
maxVal = max(values)
print (maxVal)
inMaxVal = values.index(maxVal)
print (inMaxVal)
print (values[2])
weights = [15, 8, 2, 6, 12 ]
print (f'item: {inMaxVal+1} weight: {weights[inMaxVal]}')
10
2
10
item: 3 weight: 2
values = [ 4, 7, 10, 5, 3]
inMaxVal = values.index(max(values))
weights = [15, 8, 2, 6, 12 ]
print (f'item: {inMaxVal+1} weight: {weights[inMaxVal]}')
item: 3 weight: 2
for item in range(len(weights)):
itemValue = max(values)
maxIndex = values.index(itemValue)
itemWeight = weights[maxIndex]
print (f"Choose item number {maxIndex+1} with index {maxIndex}, value = {itemValue} and weight = {itemWeight}")
values[maxIndex]=0
#
# set value to zero so we don't find it again
Choose item number 2 with index 1, value = 7 and weight = 8
Choose item number 4 with index 3, value = 5 and weight = 6
Choose item number 1 with index 0, value = 4 and weight = 15
Choose item number 5 with index 4, value = 3 and weight = 12
Choose item number 1 with index 0, value = 0 and weight = 15
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
backWeight = 0
for item in range(len(weights)):
itemValue = max(values)
maxIndex = values.index(itemValue)
itemWeight = weights[maxIndex]
print (f"Choose item number {maxIndex+1} with index {maxIndex}, value = {itemValue} and weight = {itemWeight}")
backWeight += itemWeight
print (backWeight)
values[maxIndex]=0
Choose item number 3 with index 2, value = 10 and weight = 2
2
Choose item number 2 with index 1, value = 7 and weight = 8
10
Choose item number 4 with index 3, value = 5 and weight = 6
16
Choose item number 1 with index 0, value = 4 and weight = 15
31
Choose item number 5 with index 4, value = 3 and weight = 12
43