Question: Your code for this problem should be in a file called Knapsack-py. Imagine that you're invited to enter a vault and help yourself to as

 Your code for this problem should be in a file called

Your code for this problem should be in a file called Knapsack-py. Imagine that you're invited to enter a vault and help yourself to as much as you can pack into your knapsack. Your knapsack, of course, has limited weight capacity. Each item in the vault is marked with its weight and value. Your objective is to select a subset of the items in the vault such that their total weight does not exceed your knapsack's capacity and such that the total value of the items that you choose is maximized. Each item can only be used once. Here is an example of us defining some items in Python: >rock [160, 4] u Heavy and low value " Light and very valuable! # Very light weight and some value > diamond [3, 25 spam [2, 1 >>> stuff " [rock, diamond, span) # now make a list of those items >> stuff (16e, 4], [3, 25), [2, 1] Of course, we could have just defined stuff directly as stuff" [ [16e, 4], [3, 25], [2, 111 Your job is to write a function knapsack(capacity, items) that takes a positive integer capacity and a list of items (such as stuff in the example above) where each item in the list is itself a list of the form [weight, value]. Then knapsack returns the best solution, which is maximum total value of items you can pack. Below is an example: > knapsack(5, stuff) knapsack capacity is s 26 .. The optimal value is 26 (uses the diamond and span) Of course, we could have also run this without first defining stuff as in: knapsack(s, 26 (6e, 4], [3, 25], (2, 1]1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!