values = [ 4, 7, 10, 5, 3]
print (f' the max value is {max(values)} with index {values.index(max(values))}')
values [2]
weights = [15, 8, 2, 6, 12 ]
weights [2]
max_weight = sum(weights)
print (f' total weight of the knapsack will be {max_weight}')
current_value = 0
current_weight = 0
n= len(weights)
# set value to zero so we don't find it again
for item in range (0, n) :
print ('*******************************************************')
item_value = max(values)
print ('item value', item_value)
max_index = values.index(item_value)
item_weight = weights[max_index]
print (f"choose item {max_index +1} with max value {item_value} and weight {item_weight}")
if (current_weight + item_weight <= max_weight) :
print (f' add item number {item +1} to backpack with weight {item_weight}')
print (f' the bag is underweight ')
print (f' item {max_index + 1} was added to backpack with value = {item_value} ')
current_weight = current_weight + item_weight
current_value = current_value + item_value
print (f' current weight in backpack is {current_weight} with a value of {current_value}')
values[max_index] = 0
print (f' total weight is {current_weight} with a value of {current_value}')