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 bookstore.Write a program to calculate the maximum number of books you can buy in each store with the corresponding pocket 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 represent the details of a shop. The first line of each set contains two space separated integers B and P. The second line of each set contains B space separated integers.
Output:
The output should be T lines. Each line contains an integer that represents the maximum number of books that can be bought in each shop.
def getCountBooks(money, books):
count = 0
for cost in books:
money -= int(cost)
if money >= 0:
count += 1
return count
print('Enter the number of stores')
stores = int(input())
data = []
for i in range(stores):
print(f'Store #{i+1}')
print('Enter the amount of your money')
money = int(input())
print('Enter a list of book prices in the store')
prices = input().split(' ')
prices.sort(key=int)
data.append([money, prices])
num = 1
for info in data:
countBooks = getCountBooks(info[0], info[1])
print(f'In the store #{num} can buy a maximum of {countBooks} books')
Comments
Leave a comment