Answer to Question #320733 in Python for sandhya

Question #320733

Maximum Number of Books:

There are multiple(T)book stores in the area each shopkeeper has a list of B integers that represents the cost of each book you have different pocket money(p) for each book store. write a program to calculate the maximum number of books you can buy in each store with the corresponding packet money.

input:

The first line of input is a positive integer T that represents the number of shops.

The following lines represent the details of the T shops.

Each set of two consecutive lines represents the details of a shop.

The first line of each set contains two space-seperated integers B & P

The second line of each set contains B space-seperated integers.

input:

3

6 100

20 40 10 80 20 10

4 70

20 10 300 30

4 200

220 1000 500 2000

output:

5

3


input:

2

8 250

2070 1350 365 2750 30 20 140 240

4 500

1070 2893 2200 39

output:

3

1





1
Expert's answer
2022-03-30T07:39:13-0400
def bookprice(b , N, p):
    count = 0
    sum = 0
 

    b.sort(reverse = False)
    for i in range(0, N, 1):
         
        
        if (sum+b[i] <= p):
            sum = sum + b[i]

            count += 1
     
    return count
 
p = 70
b = [35,32,23]
N = len(b)
 
print(bookprice(b, N, p))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS