Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def shopping_cart(*args):
- result = ""
- meal = {"Pizza": [], "Dessert": [], "Soup": []}
- for i in args:
- if i == "Stop":
- break
- key = i[0]
- value = i[1]
- if key == "Pizza" and len(meal["Pizza"]) < 4:
- if value not in meal["Pizza"]:
- meal["Pizza"].append(value)
- elif key == "Dessert" and len(meal["Dessert"]) < 2:
- if value not in meal["Dessert"]:
- meal["Dessert"].append(value)
- elif key == "Soup" and len(meal["Soup"]) < 3:
- if value not in meal["Soup"]:
- meal["Soup"].append(value)
- if len(meal["Pizza"]) == 0 and len(meal["Dessert"]) == 0 and len(meal["Soup"]) == 0:
- result += f"No products in the cart!"
- else:
- sorted_list = sorted(meal.items(), key=lambda x: (-len(x[1]), x[0]))
- for i, j in sorted_list:
- sorted_list_of_objects = sorted(j)
- result += f"{i}:\n"
- for obj in sorted_list_of_objects:
- result += f" - {obj}\n"
- return result
- print(shopping_cart(
- ('Pizza', 'ham'),
- ('Soup', 'carrots'),
- ('Pizza', 'cheese'),
- ('Pizza', 'flour'),
- ('Dessert', 'milk'),
- ('Pizza', 'mushrooms'),
- ('Pizza', 'tomatoes'),
- 'Stop',
- ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement