Advertisement
mbratanov

03. Shopping Cart

Oct 19th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. def shopping_cart(*args):
  2.     max_quantities = {"Soup": 3, "Pizza": 4, "Dessert": 2}
  3.     cart = {"Soup": [], "Pizza": [], "Dessert": []}
  4.  
  5.     for command in args:
  6.         if command == "Stop":
  7.             break
  8.         else:
  9.             meal = command[0]
  10.             product = command[1]
  11.             if len(cart[meal]) < max_quantities[meal]:
  12.                 if product not in cart[meal]:
  13.                     cart[meal].append(product)
  14.  
  15.     for value in cart.values():
  16.         if len(value) > 0:
  17.             break
  18.     else:
  19.         return 'No products in the cart!'
  20.  
  21.     result = ""
  22.     for meal, products in sorted(cart.items(), key=lambda item: (-len(item[1]), item[0])):
  23.         result += f"{meal}:\n"
  24.         for product in sorted(products):
  25.             result += f" - {product}\n"
  26.  
  27.     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement