SimeonTs

SUPyF2 Dictionaries-Lab - 03 Statistics

Oct 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. """
  2. Dictionaries - Lab
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1736#2
  4.  
  5. SUPyF2 Dictionaries-Lab - 03 Statistics
  6.  
  7. Problem:
  8. You seem to be doing great at your first job.
  9. You have now successfully completed the first 2 of your tasks and your boss decides to give you as your
  10. next task something more challenging. You have to accept all the new products coming in the bakery and
  11. finally gather some statistics.
  12. You will be receiving key-value pairs on separate lines separated by ": " until you receive the command "statistics".
  13. Sometimes you may receive a product more than once. In that case you have to add the new quantity to the existing one.
  14. When you receive the "statistics" command, print the following:
  15. "Products in stock:
  16. - {product1}: {quantity1}
  17. - {product2}: {quantity2}
  18. Total Products: {count_all_products}
  19. Total Quantity: {sum_all_quantities}"
  20. Example:
  21. Input:
  22. bread: 4
  23. cheese: 2
  24. ham: 1
  25. bread: 1
  26. statistics
  27.  
  28. Output:
  29. Products in stock:
  30. - bread: 5
  31. - cheese: 2
  32. - ham: 1
  33. Total Products: 3
  34. Total Quantity: 8
  35. """
  36. stock = {}
  37.  
  38. while True:
  39.     command = input().split(": ")
  40.     if command[0] == "statistics":
  41.         break
  42.     product = command[0]
  43.     value = int(command[1])
  44.     if product not in stock:
  45.         stock[product] = value
  46.     else:
  47.         stock[product] += value
  48.  
  49. print("Products in stock:")
  50. for item, value in stock.items():
  51.     print(f"- {item}: {value}")
  52. print(f"Total Products: {len(stock)}")
  53. print(f"Total Quantity: {sum(stock.values())}")
Add Comment
Please, Sign In to add comment