Discount Sale
It is a Summer Discount sale in Mumbai and all the local shops have put up various offers. Arjun selected N items to buy. While standing in the billing queue, he noticed the offer "Buy two! Get two FREE!!". This means that for every two items he buys, they give him two items for free. However, items can be of varying prices, they always charge for the two most costly items and give the other two as free. For example, if the items cost 1, 1, 2, 2, then you have to pay four rupees and take all four items.
Arjun is confused with grouping his items to reduce the total price ho has to pay. Your task is to find the minimum price Arjun has to pay to buy all the N items.
The answer to your question:
prices_str=input("Enter the prices of goods separated by a space: \n")
prices=[]
for s in prices_str.split():
prices.append(int(s))
prices.sort(reverse=True)
shp_bsk=[]
i=0
n=0
value=0
while i<len(prices)//4:
shp_lst=[]
j=0
while j<4:
shp_lst.append(prices[n])
if j<2:
value=value+prices[n]
j=j+1
n=n+1
shp_bsk.append(shp_lst)
i=i+1
i=0
shp_lst=[]
while i<len(prices)%4:
shp_lst.append(prices[n])
if i<2:
value=value+prices[n]
i=i+1
n=n+1
if i>0:
shp_bsk.append(shp_lst)
print("The minimum price Arjun has to pay to buy all the N items: "+str(value)+" rupees")
print("Optimal grouping:")
i=1
for shp_lst in shp_bsk:
print("Shopping basket #"+str(i)+" "+str(shp_lst))
i=i+1
Comments
Leave a comment