SimeonTs

SUPyF2 Lists-Advanced-Exercise - 10. Practice Sessions

Oct 10th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. """
  2. Lists Advanced - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1731#8
  4.  
  5. SUPyF2 Lists-Advanced-Exercise - 10. Practice Sessions (not included in final score)
  6.  
  7. Problem:
  8. The racers must practice for the race. Your job is to keep the records of the roads and the time for each lap.
  9. The track with the best time will be the chosen one for the finals.
  10.  
  11. Write a program, that keeps information about roads and the racers who practice on them.
  12. When the practice begins, you’re going to start receiving data until you get the "END" message.
  13. There are three possible commands:
  14. • "Add->{road}->{racer}"
  15. o   Add the road if it doesn't exist in your collection and add the racer to it.
  16. • "Move->{currentRoad}->{racer}->{nextRoad}"
  17. o   Find the racer on the current road and move him to the next one, only if he exists in the current road.
  18.    Both roads will always be valid and will already exist.
  19. • "Close->{road}"
  20. o   Find the road and remove it from the sessions, along with the racers on it if it exists.
  21. In the end, print all of the roads with the racers who have practiced and ordered by the count of the racers in
  22. descending order, then by the roads in ascending order. The output must be in the following format:
  23. Practice sessions:
  24. {road}
  25. ++{racer}
  26. ++{racer}
  27. ++{racer}
  28. ………………………..
  29. Input / Constraints
  30. • You will be receiving lines of information in the format described above, until you receive the "END" command.
  31. • The input will always be in the right format.
  32. • Both roads from the "Move" command will always be valid and you don't need to check them explicitly.
  33. Output
  34. • Print the roads with their racers in the format described above
  35.  
  36. Examples:
  37. Input:
  38. Add->Glencrutchery Road->Giacomo Agostini
  39. Add->Braddan->Geoff Duke
  40. Add->Peel road->Mike Hailwood
  41. Add->Glencrutchery Road->Guy Martin
  42. Move->Glencrutchery Road->Giacomo Agostini->Peel road
  43. Close->Braddan
  44. END
  45.  
  46. Output:
  47. Practice sessions:
  48. Peel road
  49. ++Mike Hailwood
  50. ++Giacomo Agostini
  51. Glencrutchery Road
  52. ++Guy Martin
  53.  
  54. Comments:
  55. We add racers to the roads they are racing on. When we receive the "Move" command,
  56. we check if Giacomo Agostini is on Glencrutchery Road and if he is,
  57. we remove him from it and add him to the next one - Peel road.
  58. When we receive the "Close" command, we remove Brandon road and remove all its records.
  59. In the end we print the roads sorted by the count of racers on them and then by the
  60. names of the roads in ascending order.
  61. """
  62. all_roads = []
  63.  
  64. while True:
  65.     data = input().split("->")
  66.     if data[0] == "END":
  67.         break
  68.     elif data[0] == "Add":
  69.         trace_in_all_roads = False
  70.         for trace in all_roads:
  71.             if trace[0] == data[1]:
  72.                 trace_in_all_roads = True
  73.                 trace += [data[2]]
  74.                 break
  75.         if not trace_in_all_roads:
  76.             all_roads += [[data[1], data[2]]]
  77.     elif data[0] == "Move":
  78.         for trace in all_roads:
  79.             if trace[0] == data[1]:
  80.                 if data[2] in trace:
  81.                     trace.remove(data[2])
  82.                     for trace_2 in all_roads:
  83.                         if trace_2[0] == data[3]:
  84.                             trace_2 += [data[2]]
  85.     elif data[0] == "Close":
  86.         for trace in all_roads:
  87.             if trace[0] == data[1]:
  88.                 all_roads.remove(trace)
  89.  
  90. all_roads = sorted(sorted(all_roads, key=lambda x: x[0]), key=lambda x: len(x), reverse=True)
  91.  
  92. print("Practice sessions:")
  93. for road in all_roads:
  94.     print(road[0])
  95.     for driver in road[1:]:
  96.         print(f"++{driver}")
Add Comment
Please, Sign In to add comment