Have you ever felt you've been cheated with a "buy 3 get 1 for free" action? It's quite all right if shops advertise such an action and interpret it in such a way that if you purchase four items, you get the cheapest one for free. What bothers most customers is that some shops turn this into their advantage as soon as you buy more than four products. Cash registers might be programmed to interpret a "buy 3 get 1 for free" action such that for each $$n$$ items purchased, you get the $$\lfloor\frac{n}{4}\rfloor$$ cheaptest items for free. Here, $$\lfloor x\rfloor$$ is the largest integer not greater than $$x \in \mathbb{R}$$.
Only after customers have paid all their items at once, they realize that they would have made profit if they had passed the cash register several times. They could have arranged the purchased items from the most expensive one to the cheapest one, and could have passed the cash register with groups of at most four items. The first time with the four most expensive items (for which they get the cheapest for free), a second time with the next four most expensive items, and so on. If the total number of items purchased is not a multiple of four, they would have to pass the cash register one last time with less than four items, and not get an item for free this time.
Say you have purchased a number of items in a shop that runs a "buy 3 get 1 for free" action, and whose cash register has been programmed in favor of the shop as described in the introduction of this assignment. Your task is to determine your own profit in case you would not purchase all products at once, but pass the cash register multiple times, each time getting the cheapest item for free from the group of the next four most expensive items. In order to do so, you have to write four functions that each take a sequence (list or tuple) of $$n \in \mathbb{N}_0$$ real-valued numbers (float). These numbers represent the prices of the items purchased.
Write a function together that returns the total amount (float) you would have to pay in case you passed the cash register once to purchase all items together.
Write a function group that returns a list (list) of tuples (tuple), where each tuple contains at least one and at most four real-valued numbers (float) that are arranged from the largest to the smallest. The first tuple must contain the prices of the four most expensive items, the second tuple must contain the prices of the next four most expensive items, and so on. If the number of purchased items is not a multiple of four, the last tuple must contain the prices of the remaining items.
Write a function grouped that returns the total amount (float) you would have to pay in case you passed the cash register multiple times to purchase the items in groups of at most four items, as described in the introduction of this assignment.
Write a function profit that returns the profit (float) you make if you follow the strategy where you pass the cash register multiple times, compared to the total amount you would have to pay if you only passed the cash register once to purchase all items together.
>>> prices = [3.23, 5.32, 8.23, 2.23, 9.98, 7.43, 6.43, 8.23, 4.23]
>>> together(prices)
49.85
>>> group(prices)
[(9.98, 8.23, 8.23, 7.43), (6.43, 5.32, 4.23, 3.23), (2.23,)]
>>> grouped(prices)
44.65
>>> profit(prices)
5.2