SimeonTs

SUPyF Dictionaries - 10. Shellbound

Jun 23rd, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. """
  2. Dictionaries and Functional Programming
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/945#9
  4.  
  5. SUPyF Dictionaries - 10. Shellbound
  6.  
  7. Problem:
  8. Vladi is a crab. Crabs are scared of almost anything, which is why they usually hide in their shells. Vladi
  9. is a rich crab though. He has acquired many outer shells, in different regions, in which he can hide –
  10. each with a different size.
  11. However, it is really annoying to look after all your shells when they are so spread out. That is why Vladi decided
  12. to merge all shells in each region, so that he has one big shell for every region. He needs your help to do that.
  13. You will be given several input lines containing a string and an integer, separated by a space. The string will
  14. represent the region’s name, and the integer – the shell in the given region, size.
  15. You must store all shells in their corresponding regions.
  16. If the region already exists, add the new shell to it. Make sure you have NO duplicate shells (shells with same size).
  17. Vladi doesn’t like shells to have the same size.
  18. When you receive the command “Aggregate”, you must stop reading input lines, and you must print every region, all of
  19. the shells in that region, and the size of the giant shell after you’ve merged them in the following format:
  20. {regionName} -> {shell 1, shell 2…, shell n…} ({giantShell})
  21. The giant shell size is calculated when you subtract the average of the shells from the sum of the shells.
  22. Or in other words: (sum of shells) – ((sum of shells) / (count of shells)).
  23. Constraints
  24. - All numeric data will be represented with integer variables in range [0…1.000.000.000].
  25. Examples:
  26. Input:
  27. Sofia 50
  28. Sofia 20
  29. Sofia 30
  30. Varna 10
  31. Varna 20
  32. Aggregate
  33. OutPut:
  34. Sofia -> 50, 20, 30 (67)
  35. Varna -> 10, 20 (15)
  36. """
  37. import math
  38. regions = {}
  39. while True:
  40.     command = input()
  41.     if command == "Aggregate":
  42.         break
  43.     a = [item for item in command.split(" ")]
  44.     if a[0] not in regions:
  45.         regions[a[0]] = [a[1]]
  46.     else:
  47.         if a[1] not in regions[a[0]]:
  48.             regions[a[0]] += [a[1]]
  49.         else:
  50.             continue
  51.  
  52. for region, value in regions.items():
  53.     print(f"{region} -> ", end="")
  54.     print(", ".join(value), end=" ")
  55.     sum_of_value = 0
  56.     count_value = len(value)
  57.     for i in value:
  58.         sum_of_value += int(i)
  59.     grand_crab = math.ceil(sum_of_value - (sum_of_value / count_value))
  60.     print(f"({grand_crab})")
Add Comment
Please, Sign In to add comment