Answer to Question #318278 in Python for Rajesh

Question #318278

There are multiple (T) bookstores 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 bookstore. Write a program to calculate the maximum number of books you can buy in each store with the corresponding pocket money.


Explanation

Given P = 600 and B = [120, 140, 110, 180, 120, 110] with a pocket money of 600, we can buy the books at index 0,1,2,4,5 , whose sum is 120+140+110+120+110 is equal to P.

Given P = 300 and B = [120 110 1300 130] with a pocket money of 300, we can buy the books at index 0,1 whose sum is 120 + 110 and is less than P.

Given P = 100 and B = [220 1000 500 2000] with pocket money of 100, we can't buy any book. So the output should be Retry.



Sample Input1

3

6 100

20 40 10 80 20 10

4 70

20 10 300 30

4 200

220 1000 500 2000


Sample Output1

5

3



Sample Input2

2

8 250

2070 1350 365 2750 30 20 140 240

4 500

1070 2893 2200 39


Sample Output2

3

1


1
Expert's answer
2022-03-25T15:11:49-0400
def main():
    bookstores_count = int(input())
    result = []
    for _ in range(bookstores_count):
        books_count, pockey_money = (int(i) for i in input().split(" "))
        books_cost = [float(i) for i in input().split(" ")]

        if len(books_cost) > books_count:
            raise ValueError("Incorrect number of prices!")

        books_cost = [i for i in books_cost if float(i) <= pockey_money]

        while sum(books_cost) > pockey_money:
            books_cost.remove(max(books_cost))

        result.append(len(books_cost))

    for i in result:
        if i:
            print(i)


if __name__ == "__main__":
    main()

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