Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python, I have the following code given: a=200 b=[('1', 5, 4), ('2', 1, 9), ('3', 1, 24), ('4', 46, 24), ('5', 8, 16), ('6',

In Python, I have the following code given:

a=200

b=[('1', 5, 4), ('2', 1, 9), ('3', 1, 24), ('4', 46, 24), ('5', 8, 16), ('6', 8, 9), ('7', 2, 4), ('8', 11, 6), ('9', 14, 8), ('10', 2, 9), ('11', 1, 5), ('12', 7, 8), ('13', 6, 1), ('14', 8, 4), ('15', 9, 13), ('16', 20, 9), ('17', 8, 4), ('18', 3, 1), ('19', 6, 5), ('20', 1, 7)]

Variable a represents the total capacity.

Variable b contains 20 tuples with three values:

  • The number of the tuple, eg '1'

  • Variables x

  • Variables y

The problem is a knapsack problem. I need a function called c(b): that can according to the knapsack problem store the smallest (variable x) product first and when two variables x are the same, give the one with the highest value for variable y a priority. The function should return a tuple with three elements:

  • The first element should be a string which numbers which tuple numbers are stored

  • The second element should state the sum of the variables x, which cannot exceed 200, the value of variable a

  • The third element should state the sum of the variables y.

I currently have the following code:

a=200 b=[('1', 5, 4), ('2', 1, 9), ('3', 1, 24), ('4', 46, 24), ('5', 8, 16), ('6', 8, 9), ('7', 2, 4), ('8', 11, 6), ('9', 14, 8), ('10', 2, 9), ('11', 1, 5), ('12', 7, 8), ('13', 6, 1), ('14', 8, 4), ('15', 9, 13), ('16', 20, 9), ('17', 8, 4), ('18', 3, 1), ('19', 6, 5), ('20', 1, 7)] def c(b): n = len(b) for i in range(0,n): for j in range(0, n-i-1): if (b[j][1] > b[j + 1][1] or (b[j][1] == b[j + 1][1] and b[j][2] < b[j+1 [2])): temp = b[j] b[j] = b[j+1] b[j+1] = temp tuples = "" sumx = 0 sumy = 0 for tup in b: if (sumx + tup[1] < a): if(tuples == ""): tuples = tup[0] else: tuples = tuples + ", " + tup[0] sumx = sumx + tup[1] sumy = sumy + tup[2] return (tuples,sumx,sumy) 

This is right, but something should be altered and I am completely clueless as to how to do it.

First, this print(c([('1', 5, 4), ('end', 201, 4)]) == (['1'], 5, 4)) should return True. So something with [] should be added.

Second, print(c(b)) should not return ('3, 2, 20, 11, 10, 7, 18, 1, 19, 13, 12, 5, 6, 14, 17, 15, 8, 9, 16, 4', 167, 170) but rather

(['3', '2', '20', '11', '10', '7', '18', '1', '19', '13', '12', '5', '6', '14', '17', '15', '8', '9', '16', '4'], 167, 170) 

I have tried to use .replace() or .pop() but it doesn't seem to work. Could someone help me out here?

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

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions

Question

What are the determinants of cash cycle ? Explain

Answered: 1 week ago