![scastillo8](/avatars/41733.jpg)
scastillo8
16.04.2020 •
Computers and Technology
Suppose you are an art thief (not a good start) who has broken into an art gallery. All you have to haul out your stolen art is your briefcase which holds only W pounds of art and for every piece of art, you know its weight. Write a program that contains a dynamic programming function to determine your maximum profit. Include a main program that tests your function against each of the following two data sets (each happens to have n=5 items; but your function should work for other values of n as well; we are likely to test your code against another random dataset with an unknown number of items):
Data set 1:
Items 1, 2, 3, 4, and 5 have weights 2, 3, 4, 5 , 9 and value 3, 4, 8, 8, and 10, respectively. Let W = 20.
Data set 2:
Items 1, 2, 3, 4, and 5 have weights 1, 2, 5, 6, 7 and value 1, 6, 18, 22, and 28, respectively. Let W = 11.
Solved
Show answers
More tips
- A Auto and Moto Battle for the Relocation of The Cherkizovsky Market: Who Won?...
- F Food and Cooking Homemade kvass: recipe and brewing process...
- O Other What is a Disk Emulsifier and How Does it Work?...
- H Health and Medicine How to Calm Your Nerves? Expert Tips That Actually Work...
- A Animals and plants 5 Tips for Taking Care of Yews to Keep Them Green and Beautiful...
- S Sport How to wrap boxing hand wraps? Everything you need to know!...
- F Food and Cooking 10 Reasons Why You Should Avoid Giving Re-Gifts: An Informative Guide...
- F Family and Home Tender Care for Your Parquet: Is it Possible to Clean Parquet?...
- S Style and Beauty How Are Eyelash Extensions Applied? All Your Questions Answered...
- F Food and Cooking 10 Tips for Proper Sushi Consumption...
Answers on questions: Computers and Technology
- W World Languages What does shi shi mean in English btw this mandarin...
- B Biology How can scientists determine when these ancient animals lived?...
- P Physics I. Complete with one of these phrasal verbs: hand back, take off, h 1. The planelate because of the bad weather,...
- M Mathematics Plz helpp what is b?...
- S Social Studies What three aspects does a forensic autopsy seek information about? describe each of these aspects?...
Ответ:
Check the explanation
Explanation:
weights = [2,3,4,5,9] #assign the weights here
value = [3,4,8,8,10] #assign the values here
#recursive function to get the maximum weight for a given knapsack
def getMaxWeight(weights,value,W,n):
#check if any elements are there or check if the knapsack can hold any more weights
if(n==0 or W==0):
return 0
elif(weights[n]>W):
#if the current value of the weight is greater than what a knapsack can hold then discard that
return getMaxWeight(weights,value,W,n-1)
else:
#either choose the current weight or do not choose based on the maximum values
return max(value[n]+getMaxWeight(weights,value,W-weights[n],n-1),getMaxWeight(weights,value,W,n-1))
if __name__ == '__main__':
W=20
n=len(weights)-1
print("Weights are:",weights)
print("Values are:",value)
print("Maximum value for given knapsack is:",getMaxWeight(weights,value,W,n))
Kindly check the attached output images below.
Ответ: