Question

Python Problem, just need some guidance with the description, pseudocode and run time. I believe this...

Python Problem, just need some guidance with the description, pseudocode and run time. I believe this is an 0-1 knapsack problem?

Shopping Spree: (20 points) 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.

a) A verbal description and give pseudo-code for your algorithm.

b) What is the theoretical running time of your algorithm for one test case given N items, a family of size F, and family members who can carry at most Mi pounds for 1 ≤ i ≤ F.

Input: The input file input.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 ・output.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

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

f = open("shopping.txt", "r")
outFile = open("shopping.out", "w")
t = int(f.readline().strip())
for _ in range(t):
   lis = {}
   items = int(f.readline().strip())
   ind = 1
   for i in range(items):
       p, w = map(int, f.readline().strip().split())
       lis[p]=[w, ind]
       ind+=1
   weights = []
   F = int(f.readline().strip())
   for i in range(F):
       weights.append(int(f.readline().strip()))
   RES = []
   values = []
   for weight in weights:
       sortedPrice = sorted(lis.keys())[::-1]
       m = 0
       p = 0
       tmp = []
       for i in range(len(lis)):
           R = []
           s = 0
           p = 0
           if lis[sortedPrice[i]][0]<=weight:
               s=lis[sortedPrice[i]][0]
               p=sortedPrice[i]
               R+=lis[sortedPrice[i]][1],
           for j in range(i+1, len(lis)):
               if lis[sortedPrice[j]][0]+s<=weight:
                   s+=lis[sortedPrice[j]][0]
                   p+=sortedPrice[j]
                   R+=lis[sortedPrice[j]][1]
           if m<p:
               m = p
               tmp = R
               tmp.sort()
               RES.append(tmp)
               values.append(m)
outFile.write("Test Case %d"%(_+1))
outFile.write("Total Price: %d"%(sum(values)))
outFile.write("Member Items")
for i in range(len(RES)):
   outFile.write("%d: %s"%(i+1, " ".join(map(str, RES[i]))))
f.close()
outFile.close()

Add a comment
Know the answer?
Add Answer to:
Python Problem, just need some guidance with the description, pseudocode and run time. I believe this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Shopping Spree: Acme Super Store is having a contest to give away shopping sprees to lucky...

    Shopping Spree: 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...

  • "Greedy, but Better": Given a knapsack problem with a weight capacity C, and n items, and...

    "Greedy, but Better": Given a knapsack problem with a weight capacity C, and n items, and each item has a weight W[1:n] and monetary value P[1:n]. You have to determine which items to take so that the total weight is C, and the total value (profit) is maximized. In this case we are considering an integer problem, so you can either take an item, or not take an item, you cannot take it fractionally. If you recall, the greedy algorithm...

  • There are n items in a store. For each item i=1,2,...,n the weight of the item...

    There are n items in a store. For each item i=1,2,...,n the weight of the item is wi and the value of the item is vi. A thief is carrying a knapsack of weight W. In this version of a problem the items can be broken into smaller pieces, so the thief may decide to carry only a fraction xi of object i, where 0≤xi ≤1. Item i contributes xiwi to the total weight in the knapsack, and xivi to...

  • Below is the code for the class shoppingList, I need to enhance it to accomplish the...

    Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList {   private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...

  • Write a Pseudocode for the below implementation of Greedy Algorithm to find the number of the...

    Write a Pseudocode for the below implementation of Greedy Algorithm to find the number of the least truck can be used to deliver items in C++. #include <iostream> using namespace std; void TruckFunction(int n , int w[], int limit) { int trucks = 1; int weight = 0; cout<<endl<<"Items carried in truck " <<trucks<<": "<<endl; for ( int i = 0 ; i < n ; i ++ ) { if((weight + w[i]) <= limit) { weight += w[i]; cout<<"Item...

  • (2) Suppose, the game is updated so that every item, i, now has weight, wi], in kilograms (you ca...

    Please help with question 2 (c). (2) Suppose, the game is updated so that every item, i, now has weight, wi], in kilograms (you can assume you are passed the item weights in an array, w, of size m). You can only carry n kilograms of weight. (a) (1 point) Example: Let n Ξ 15, m 5. If the item values are v (5.30, 17, 32, 40) and the item weights are w - 2,4, 3,6,15} which should you choose...

  • Recall that in the "Knapsack Problem", there are n items having respective values V1..n) and weights...

    Recall that in the "Knapsack Problem", there are n items having respective values V1..n) and weights W1..n), all greater than 0 and one needs to maximize the total value of the subset of the items placed in the knapsack limited by a weight capacity of W In the 0-1 Knapsack Problem, each item must be either be included or excluded in its entirety, in light of the fact that this problem is to be "NP-Complete", how can one solve the...

  • ..Construct a solution algorithm for the following problem. Your solution should contain \: .Defi...

    ..Construct a solution algorithm for the following problem. Your solution should contain \: .Defining diagram .Pesudo code algorithm .Desk checking of the algorithm (three test case for each question including one "Invalid" Desk Checking) A transaction record on a sales commission file contains the retail price of an item sold, a transaction code which indicates the sales commission category to which an item can belong and the employee number of the person who sold the item. The transaction code can...

  • I am stuck with this coding problem from edx coding python 4.4.6: #This is a long...

    I am stuck with this coding problem from edx coding python 4.4.6: #This is a long one -- our answer is 20 lines of code, but #yours will probably be longer. That's because it's one of the #more authentic problems we've done so far. This is a real #problem you'll start to face if you want to start creating #useful programs. # #One of the reasons that filetypes work is that everyone #agrees how they are structured. A ".png" file,...

  • C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an...

    C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an ad about the new cafe. The promotion is if the price of an item is N dollars, then you can buy the second item for half the price, the third item for a quarter of the original price, and so on, but if it becomes less than M dollars, then you have to pay M dollars. He wonders how much he has to pay...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT