Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Imagine you are a treasure hunter and given a knapsack that can hold a certain weight capacity. You will be exploring a far - away

Imagine you are a treasure hunter and given a knapsack that can hold a certain weight capacity. You will
be exploring a far-away land for long lost gold bars. The gold bars will be of varying weight. You cannot
take more gold bars than your knapsack can hold, but you obviously want to take as much gold as
possible. You also cannot take fractions of the gold bars.
Write a program such that it will receive (as inputs) an array of gold bar weights and a knapsack
capacity, and it will output the maximum sum weight of the gold bars that can be taken.
The unit of weight is irrelevant (you can assume they are all kilogram or pounds, it doesnt matter). In
addition, the weight and capacity will always be an integer.
For example, if the array of gold bar weights is [32,7,9,26] and the knapsack capacity is 34, then you can
take a maximum of 33 units of weight of gold. The weights of gold bars taken in this example is 26 and 7.
If there are not any gold bars that can be taken, your program should return 0.
You cannot import any library in your code. Your code must have a function called max_gold and that
will take in two inputs: the array of gold weights and the knapsack capacity.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions