Deals on Cookies ---------------- It is lunch time again and I am starving. I wo
ID: 3847773 • Letter: D
Question
Deals on Cookies ---------------- It is lunch time again and I am starving. I would like to buy some cookies from a small Cafe, named Cafe CS201, owned and operated by CSC201. Cafe CS201 offen offers discounts for buying multiple cookies. I wonder which of the discounts provides the best values. For this problem, please write a program to help me find the best deal. Input The input contains multiple scenarios. Each scenario starts with a line containing two numbers N ( 1<= N <=10) and M( 1<= M <=20), N means there are N deals available, M means the max amount of cookies I want to buy (I may buy less if I get the best deal). Following N lines, each line has two numbers: x and y which represent an offer to buy x cookies for $y. The input will terminated by a line containing the characters 0 0. Output For each scenarios, print Purchase x cookies for $y for the best offer that matches requirements (the best deal but less or equal to the number of cookies I want to buy). If there are multiple best offers, print the one which buys more tickets (but still be less or equals to the number of cookies I want to buy). If there is no suitable offer (may require to buy more cookies than I want to), print No good deals. Sample input data Sample Output ------------------------------------------------------------------ 5 6 Purchase 6 cookies for $6 12 10 Purchase 3 cookies for $5 9 8 Purchase 1 cookies for $3 6 6 Purchase 2 cookies for $4 3 3 No good deals 1 2 3 5 1 3 3 5 4 7 3 2 3 5 1 3 4 7 3 2 3 6 1 2 2 4 1 3 4 10 0 0
Explanation / Answer
As the programming language is not mentioned I am doing it in Python programming language. Please feel free to comment if any other code is required for this question.
Code:
# reading the file and taking the values into a list
import re
a=open("input.txt","r")
b=a.readlines()
arr = []
for i in b:
d=re.findall(r'd+', i)
arr.append([int(i) for i in d])
# arr is the nested array with all the deals in file
print(" ")
#taking inputs from the user
N=int(raw_input("Enter the number of deals: "))
M=int(raw_input("Enter the number of cookies: "))
#checking for the best deals in the limit of user choice
for i in range(N):
if(arr[i][0]<=M):
#printing the output which suits for the user choice
print("Purchase %d cookies for $%d"%(arr[i][0],arr[i][1]))
else:
if(arr[i][0]==0):
print("No good deals")
Sample output1:
Enter the number of deals: 5
Enter the number of cookies: 5
Purchase 5 cookies for $6
Purchase 3 cookies for $3
Sample Output2:
Enter the number of deals: 6
Enter the number of cookies: 5
Purchase 5 cookies for $6
Purchase 3 cookies for $3
Purchase 1 cookies for $2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.