values = [ 4, 7, 10, 5, 3]
item_value=max(values)
print("The item value", item_value,"occurs at index",values.index(item_value))
print(values)
values = [ 4, 7, 10, 5, 3]
weights = [15, 8, 2, 6, 12 ]
n=len(weights)
for item in range(0,n):
item_value=max(values)
max_index=values.index(item_value)
item_weight=weights[max_index]
print(f"The weight corresponding with the max value {item_value} is {item_weight}")
values = [ 4, 7, 0, 5, 3]
weights = [15, 8, 2, 6, 12 ]
#
# set value to zero so we don't find it again
for item in range(0,n):
item_value=max(values)
max_index=values.index(item_value)
item_weight=weights[max_index]
print(f"The weight corresponding with the max value {item_value} is {item_weight}")
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
values = [ 4, 7, 10, 5, 3]
weights = [15, 8, 2, 6, 12 ]
n=len(weights)
max_weight=10
cur_weight=0
cur_value=0
for item in range(0,n):
item_value=max(values)
max_index=values.index(item_value)
item_weight=weights[max_index]
if(cur_weight+item_weight<=max_weight):
cur_weight=cur_weight+item_weight
cur_value=cur_value+item_value
if(cur_weight==max_weight):
break
else:
print(f"Item number {max_index+1} doesn't fit")
values[max_index]=0