Advertisement
pacho_the_python

task_3

Jun 25th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. def shopping_cart(*args):
  2.     result = ""
  3.     meal = {"Pizza": [], "Dessert": [], "Soup": []}
  4.     for i in args:
  5.         if i == "Stop":
  6.             break
  7.         key = i[0]
  8.         value = i[1]
  9.         if key == "Pizza" and len(meal["Pizza"]) < 4:
  10.             if value not in meal["Pizza"]:
  11.                 meal["Pizza"].append(value)
  12.         elif key == "Dessert" and len(meal["Dessert"]) < 2:
  13.             if value not in meal["Dessert"]:
  14.                 meal["Dessert"].append(value)
  15.         elif key == "Soup" and len(meal["Soup"]) < 3:
  16.             if value not in meal["Soup"]:
  17.                 meal["Soup"].append(value)
  18.  
  19.     if len(meal["Pizza"]) == 0 and len(meal["Dessert"]) == 0 and len(meal["Soup"]) == 0:
  20.         result += f"No products in the cart!"
  21.     else:
  22.         sorted_list = sorted(meal.items(), key=lambda x: (-len(x[1]), x[0]))
  23.         for i, j in sorted_list:
  24.             sorted_list_of_objects = sorted(j)
  25.             result += f"{i}:\n"
  26.             for obj in sorted_list_of_objects:
  27.                 result += f" - {obj}\n"
  28.     return result
  29.  
  30.  
  31. print(shopping_cart(
  32.     ('Pizza', 'ham'),
  33.     ('Soup', 'carrots'),
  34.     ('Pizza', 'cheese'),
  35.     ('Pizza', 'flour'),
  36.     ('Dessert', 'milk'),
  37.     ('Pizza', 'mushrooms'),
  38.     ('Pizza', 'tomatoes'),
  39.     'Stop',
  40. ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement