JulianJulianov

infoRoadsRacers

Jul 20th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.79 KB | None | 0 0
  1. Problem 2. Practice sessions
  2. The racers must practice for the race. Your job is to keep the records of the roads and the time for each lap. The track with the best time will be the chosen one for the finals.
  3.  
  4. Write a program, that keeps information about roads and the racers who practice on them.  When the practice begins, you’re going to start receiving data until you get the "END" message. There are three possible commands:
  5. "Add->{road}->{racer}"
  6. o   Add the road if it doesn't exist in your collection and add the racer to it.
  7. • "Move->{currentRoad}->{racer}->{nextRoad}"
  8. o   Find the racer on the current road and move him to the next one, only if he exists in the current road. Both roads will always be valid and will already exist.
  9. • "Close->{road}"
  10. o   Find the road and remove it from the sessions, along with the racers on it if it exists.
  11. In the end, print all of the roads with the racers who have practiced and ordered by the count of the racers in descending order, then by the roads in ascending order. The output must be in the following format:
  12. Practice sessions:
  13. {road}
  14. ++{racer}
  15. ++{racer}
  16. ++{racer}
  17. ………………………..
  18. Input / Constraints
  19. • You will be receiving lines of information in the format described above, until you receive the "END" command.
  20. • The input will always be in the right format.
  21. • Both roads from the "Move" command will always be valid and you don't need to check them explicitly.
  22. Output
  23. • Print the roads with their racers in the format described above.
  24. Examples
  25. Input                                                        Output
  26. Add->Glencrutchery Road->Giacomo Agostini                    Practice sessions:
  27. Add->Braddan->Geoff Duke                                     Peel road
  28. Add->Peel road->Mike Hailwood                                ++Mike Hailwood
  29. Add->Glencrutchery Road->Guy Martin                          ++Giacomo Agostini
  30. Move->Glencrutchery Road->Giacomo Agostini->Peel road        Glencrutchery Road
  31. Close->Braddan                                               ++Guy Martin
  32. END
  33.  
  34. Comments
  35. We add racers to the roads they are racing on. When we receive the "Move" command, we check if Giacomo Agostini is on Glencrutchery Road and if he is, we remove him from it and add him to the next one - Peel road.
  36. When we receive the "Close" command, we remove Brandon road and remove all its records. In the end we print the roads sorted by the count of racers on them and then by the names of the roads in ascending order.
  37.  
  38. Add->Glen Vine->Steve Hislop                                  Practice sessions:
  39. Add->Ramsey road->John McGuinness                             Braddan
  40. Add->Glen Vine->Ian Hutchinson                                ++Geoff Duke
  41. Add->Ramsey road->Dave Molyneux                               ++Geoff Duke
  42. Move->Ramsey road->Hugh Earnsson->Glen Vine                   ++Mike Hailwood
  43. Add->A18 Snaefell mountain road->Mike Hailwood                Glen Vine
  44. Add->Braddan->Geoff Duke                                      ++Steve Hislop
  45. Move->A18 Snaefell mountain road->Mike Hailwood->Braddan      ++Ian Hutchinson
  46. Move->Braddan->John McGuinness->Glen Vine                     Ramsey road
  47. Close->A18 Snaefell mountain road                             ++John McGuinness
  48. END                                                           ++Dave Molyneux
  49.  
  50.  
  51. using System;
  52. using System.Collections.Generic;
  53. using System.Linq;
  54.  
  55. namespace FinalExam14042019
  56. {
  57.     class Program
  58.     {
  59.         static void Main(string[] args)
  60.         {
  61.             var infoRoadsRacers = new Dictionary<string, List<string>>();
  62.             var inputData = "";
  63.             while (!(inputData = Console.ReadLine()).Equals("END"))
  64.             {
  65.                 var command = inputData.Split("->");
  66.                 switch (command[0])
  67.                 {
  68.                     case "Add":
  69.                         if (!infoRoadsRacers.ContainsKey(command[1]))
  70.                         {
  71.                             infoRoadsRacers.Add(command[1], new List<string>());
  72.                         }
  73.                         infoRoadsRacers[command[1]].Add(command[2]);
  74.                         break;
  75.                     case "Move":
  76.                         if (infoRoadsRacers[command[1]].Contains(command[2]))
  77.                         {
  78.                             infoRoadsRacers[command[3]].Add(command[2]);
  79.                             infoRoadsRacers[command[1]].Remove(command[2]);
  80.                         }
  81.                         break;
  82.                     case "Close":
  83.                         if (infoRoadsRacers.ContainsKey(command[1]))
  84.                         {
  85.                             infoRoadsRacers.Remove(command[1]);
  86.                         }
  87.                         break;
  88.                 }
  89.             }
  90.             Console.WriteLine("Practice sessions:");
  91.             foreach (var road in infoRoadsRacers.OrderByDescending(x => x.Value.Count).ThenBy(x => x.Key))
  92.             {
  93.                 Console.WriteLine(road.Key);
  94.                 foreach (var racer in road.Value)
  95.                 {
  96.                     Console.WriteLine($"++{racer}");
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment