+--------------+---------+
|Column Name |Type|+--------------+---------+
| topping_name | varchar || cost | decimal |+--------------+---------+
topping_name is the primarykeyfor this table.
Eachrowof this tablecontains topping name and the cost of the topping.
Write a solution to calculate the total cost of all possible3-topping pizza combinations from a given list of toppings. The total cost of toppings must be rounded to 2decimal places.
Note:
Do not include the pizzas where a topping is repeated. For example, ‘Pepperoni, Pepperoni, Onion Pizza’.
Toppings must be listed in alphabetical order. For example, ‘Chicken, Onions, Sausage’. ‘Onion, Sausage, Chicken’ is not acceptable.
Return the result table ordered by total cost indescendingorder and combination of toppings inascending order.
Input:
Toppings table:+--------------+------+| topping_name | cost |+--------------+------+| Pepperoni |0.50|| Sausage |0.70|| Chicken |0.55|| Extra Cheese |0.40|+--------------+------+Output:
+--------------------------------+------------+| pizza | total_cost |+--------------------------------+------------+| Chicken,Pepperoni,Sausage |1.75|| Chicken,Extra Cheese,Sausage |1.65|| Extra Cheese,Pepperoni,Sausage |1.60|| Chicken,Extra Cheese,Pepperoni |1.45|+--------------------------------+------------+Explanation:
There are only four different combinations possible with the three topings:- Chicken, Pepperoni, Sausage: Total cost is $1.75(Chicken $0.55, Pepperoni $0.50, Sausage $0.70).- Chicken, Extra Cheese, Sausage: Total cost is $1.65(Chicken $0.55, Extra Cheese $0.40, Sausage $0.70).- Extra Cheese, Pepperoni, Sausage: Total cost is $1.60(Extra Cheese $0.40, Pepperoni $0.50, Sausage $0.70).- Chicken, Extra Cheese, Pepperoni: Total cost is $1.45(Chicken $0.55, Extra Cheese $0.40, Pepperoni $0.50).Output table is ordered by the total cost in descending order.
We need to generate all unique 3-topping combinations, sum their costs, and output the results sorted by total_cost (descending) and pizza name (ascending). This is a classic self-join/combinatorics problem in SQL, and a combinations problem in pandas. Use t1, t2, t3 aliases to enforce alphabetical ordering on topping_name.
Use a triple self-join on the Toppings table (with t1, t2, t3 aliases) and enforce alphabetical order via t1.topping_name < t2.topping_name < t3.topping_name to generate all unique pizza combinations.
Sum the cost columns, round to 2 decimals into total_cost, and concatenate the topping_names in alphabetical order to produce the pizza string.
Sort the result by total_cost descending and pizza ascending.