Question

In: Computer Science

Python Knapsack Problem: Acme Super Store is having a contest to give away shopping sprees to...

Python Knapsack Problem:

Acme Super Store is having a contest to give away shopping sprees to lucky families. If a family wins a shopping spree each person in the family can take any items in the store that he or she can carry out, however each person can only take one of each type of item. For example, one family member can take one television, one watch and one toaster, while another family member can take one television, one camera and one pair of shoes.

Each item has a price (in dollars) and a weight (in pounds) and each person in the family has a limit in the total weight they can carry. Two people cannot work together to carry an item. Your job is to help the families select items for each person to carry to maximize the total price of all items the family takes. Write an algorithm to determine the maximum total price of items for each family and the items that each family member should select.

***In python:***

Implement your algorithm by writing a program named “shopping.py”. The program should satisfy the specifications below.

Input: The input file named “shopping.txt” consists of T test cases

T (1 ≤ T ≤ 100) is given on the first line of the input file.

Each test case begins with a line containing a single integer number N that indicates the number of items (1 ≤ N ≤ 100) in that test case

Followed by N lines, each containing two integers: P and W. The first integer (1 ≤ P ≤ 5000) corresponds to the price of object and the second integer (1 ≤ W ≤ 100) corresponds to the weight of object.

The next line contains one integer (1 ≤ F ≤ 30) which is the number of people in that family.

The next F lines contains the maximum weight (1 ≤ M ≤ 200) that can be carried by the ith person in the family (1 ≤ i ≤ F).

Output: Written to a file named “results.txt”. For each test case your program should output the maximum total price of all goods that the family can carry out during their shopping spree and for each the family member, numbered 1 ≤ i ≤ F, list the item numbers 1 ≤ N ≤ 100 that they should select.

Sample Input from input file

2

3

72 17

44 23

31 24

1

26

6

64 26

85 22

52 4

99 18

39 13

54 9

4

23

20

20

36

Sample Output:

Test Case 1

Total Price 72

Member Items

1: 1

Test Case 2

Total Price 568

Member Items

1: 3 4

2: 3 6

3: 3 6

4: 3 4 6

Solutions

Expert Solution

SOLUTION :

CONSIDERING THE CONDITIONS AND REQUIREMENTS FROM THE QUESTION.

HERE CODE :

TestCase=int(input())
for i in range(TestCase):
items=int(input())
items_Price=[]
items_Weight=[]
for j in range(items):
val1,val2=input().split()
items_Price.append(val1)
items_Weight.append(val2)
familyMembers=int(input())
familyMembers_Weight=[]
for k in range(familyMembers):
weight=int(input())
familyMembers_Weight.append(weight)
print("Test Case "+str(i+1))
temp_list=items_Price[:]
total_price=int("0")
total_items=[]
for x in range(familyMembers):
total_items.append("0")
for f in range(familyMembers):
temp_list.sort()
length=len(items_Price)
while(length>=0):
Max_price=temp_list[length-1]
Max_index=items_Price.index(Max_price)
if(int(items_Weight[Max_index])<=familyMembers_Weight[f]):
total_price=total_price+int(Max_price)
total_items[f]=int(total_items[f])+1
familyMembers_Weight[f]=familyMembers_Weight[f]-int(items_Weight[Max_index])
length=length-1
print("Total Price "+str(total_price))
print("Member : Items")
for f in range(familyMembers):
print(" "+str(f+1)+" : "+str(total_items[f]))

NOTE : PLEASE UPVOTE ITS VERY MUCH NECESSARY FOR ME A LOT.. PLZZZ......


Related Solutions

Python Knapsack Problem: Acme Super Store is having a contest to give away shopping sprees to...
Python Knapsack Problem: Acme Super Store is having a contest to give away shopping sprees to lucky families. If a family wins a shopping spree each person in the family can take any items in the store that he or she can carry out, however each person can only take one of each type of item. For example, one family member can take one television, one watch and one toaster, while another family member can take one television, one camera...
Python Knapsack Problem: Acme Super Store is having a contest to give away shopping sprees to...
Python Knapsack Problem: Acme Super Store is having a contest to give away shopping sprees to lucky families. If a family wins a shopping spree each person in the family can take any items in the store that he or she can carry out, however each person can only take one of each type of item. For example, one family member can take one television, one watch and one toaster, while another family member can take one television, one camera...
Problem: Acme Super Store is having a contest to give away shopping sprees to lucky families....
Problem: Acme Super Store is having a contest to give away shopping sprees to lucky families. If a family wins a shopping spree each person in the family can take any items in the store that he or she can carry out, however each person can only take one of each type of item. For example, one family member can take one television, one watch and one toaster, while another family member can take one television, one camera and one...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a class named GroceryItem. This class should contain: - an __init__ method that initializes the item’s name and price - a get_name method that returns the item’s name - a get_price method that returns the item’s price - a __str__ method that returns a string representation of that GroceryItem - an unimplemented method is_perishable Save this class in GroceryItem.py. 2. Create a subclass Perishable that...
ALL IN PYTHON PLEASE Problem 1: Accept one integer from the user and store that information...
ALL IN PYTHON PLEASE Problem 1: Accept one integer from the user and store that information in a variable called endNo. Use an IF structure to check that endNois a positive number. If endNo is negative, print an error message and stop. If it is positive, use a loop (for or while) to generate odd numbers from 1 to endNo. Calculate the sum of these numbers using an accumulator. Rubric: Data input and data type conversions: 2.5 pts Use of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT