Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem 2. Practice sessions
- 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.
- 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:
- • "Add->{road}->{racer}"
- o Add the road if it doesn't exist in your collection and add the racer to it.
- • "Move->{currentRoad}->{racer}->{nextRoad}"
- 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.
- • "Close->{road}"
- o Find the road and remove it from the sessions, along with the racers on it if it exists.
- 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:
- Practice sessions:
- {road}
- ++{racer}
- ++{racer}
- ++{racer}
- ………………………..
- Input / Constraints
- • You will be receiving lines of information in the format described above, until you receive the "END" command.
- • The input will always be in the right format.
- • Both roads from the "Move" command will always be valid and you don't need to check them explicitly.
- Output
- • Print the roads with their racers in the format described above.
- Examples
- Input Output
- Add->Glencrutchery Road->Giacomo Agostini Practice sessions:
- Add->Braddan->Geoff Duke Peel road
- Add->Peel road->Mike Hailwood ++Mike Hailwood
- Add->Glencrutchery Road->Guy Martin ++Giacomo Agostini
- Move->Glencrutchery Road->Giacomo Agostini->Peel road Glencrutchery Road
- Close->Braddan ++Guy Martin
- END
- Comments
- 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.
- 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.
- Add->Glen Vine->Steve Hislop Practice sessions:
- Add->Ramsey road->John McGuinness Braddan
- Add->Glen Vine->Ian Hutchinson ++Geoff Duke
- Add->Ramsey road->Dave Molyneux ++Geoff Duke
- Move->Ramsey road->Hugh Earnsson->Glen Vine ++Mike Hailwood
- Add->A18 Snaefell mountain road->Mike Hailwood Glen Vine
- Add->Braddan->Geoff Duke ++Steve Hislop
- Move->A18 Snaefell mountain road->Mike Hailwood->Braddan ++Ian Hutchinson
- Move->Braddan->John McGuinness->Glen Vine Ramsey road
- Close->A18 Snaefell mountain road ++John McGuinness
- END ++Dave Molyneux
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace FinalExam14042019
- {
- class Program
- {
- static void Main(string[] args)
- {
- var infoRoadsRacers = new Dictionary<string, List<string>>();
- var inputData = "";
- while (!(inputData = Console.ReadLine()).Equals("END"))
- {
- var command = inputData.Split("->");
- switch (command[0])
- {
- case "Add":
- if (!infoRoadsRacers.ContainsKey(command[1]))
- {
- infoRoadsRacers.Add(command[1], new List<string>());
- }
- infoRoadsRacers[command[1]].Add(command[2]);
- break;
- case "Move":
- if (infoRoadsRacers[command[1]].Contains(command[2]))
- {
- infoRoadsRacers[command[3]].Add(command[2]);
- infoRoadsRacers[command[1]].Remove(command[2]);
- }
- break;
- case "Close":
- if (infoRoadsRacers.ContainsKey(command[1]))
- {
- infoRoadsRacers.Remove(command[1]);
- }
- break;
- }
- }
- Console.WriteLine("Practice sessions:");
- foreach (var road in infoRoadsRacers.OrderByDescending(x => x.Value.Count).ThenBy(x => x.Key))
- {
- Console.WriteLine(road.Key);
- foreach (var racer in road.Value)
- {
- Console.WriteLine($"++{racer}");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment