Answer to Question #349532 in Python for Sai

Question #349532

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.

1
Expert's answer
2022-06-09T18:21:51-0400

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

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