Advertisement
SimeonTs

SUPyF Dictionaries - 06. Exam Shopping

Jun 23rd, 2019
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. """
  2. Dictionaries and Functional Programming
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/945#5
  4.  
  5. SUPyF Dictionaries - 06. Exam Shopping
  6.  
  7. Problem:
  8. A supermarket has products which have quantities. Your task is to stock the shop before Exam Sunday.
  9. Until you receive the command “shopping time”, add the various products to the shop’s inventory,
  10. keeping track of their quantity (for inventory purposes). When you receive the aforementioned command,
  11. the students start pouring in before the exam and buy various products.
  12. The format for stocking a product is: “stock {product} {quantity}”.
  13. The format for buying a product is: “buy {product} {quantity}”.
  14. If a student tries to buy a product, which doesn’t exist, print “{product} doesn't exist”. If it does exist,
  15. but it’s out of stock, print “{product} out of stock”. If a student tries buying more than the quantity of that product,
  16. sell them all the quantity of the product (the product is left out of stock, don’t print anything).
  17. When you receive the command “exam time”, your task is to print the remaining inventory in the following format:
  18. “{product} -> {quantity}”. If a product is out of stock, do not print it.
  19. Examples:
  20. INPUT:
  21. stock Boca_Cola 16
  22. stock Kay's_Chips 33
  23. stock Lobster_Energy 60
  24. stock Boca_Cola 4
  25. stock Loreni 15
  26. stock Loreni 15
  27. stock Loreni 15
  28. stock Loreni 15
  29. shopping time
  30. buy Boca_Bola 2
  31. buy Lobster_Energy 20
  32. buy Boca_Cola 1
  33. buy Boba_Bola 12
  34. exam time
  35.  
  36. OUTPUT:
  37. Boca_Bola doesn't exist
  38. Boba_Bola doesn't exist
  39. Boca_Cola -> 19
  40. Kay's_Chips -> 33
  41. Lobster_Energy -> 40
  42. Loreni -> 60
  43. """
  44.  
  45. stock = {}
  46.  
  47. while True:
  48.     command = [item for item in input().split(" ")]
  49.     if "shopping" in command:
  50.         break
  51.     if command[1] not in stock:
  52.         stock[command[1]] = int(command[2])
  53.     elif command[1] in stock:
  54.         stock[command[1]] += int(command[2])
  55. while True:
  56.     command = [item for item in input().split(" ")]
  57.     if "exam" in command:
  58.         break
  59.     if command[1] not in stock:
  60.         print(f"{command[1]} doesn't exist")
  61.     elif command[1] in stock and stock.__getitem__(command[1]) > 0:
  62.         stock[command[1]] -= int(command[2])
  63.         if stock.__getitem__(command[1]) <= 0:
  64.             stock[command[1]] = 0
  65.     elif command[1] in stock and stock.__getitem__(command[1]) <= 0:
  66.         print(f"{command[1]} out of stock")
  67.  
  68. for item, value in stock.items():
  69.     if stock.__getitem__(item) > 0:
  70.         print(f"{item} -> {value}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement