Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace World_Tour
- {
- internal static class Program
- {
- private static void Main()
- {
- var route = Console.ReadLine();
- string command;
- while ((command = Console.ReadLine()) != "Travel")
- {
- var tokens = command.Split(":").ToArray();
- route = tokens[0] switch
- {
- "Add Stop" => AddStop(route, int.Parse(tokens[1]), tokens[2]),
- "Remove Stop" => RemoveStop(route, int.Parse(tokens[1]), int.Parse(tokens[2])),
- "Switch" => ReplaceStops(route, tokens[1], tokens[2]),
- _ => route
- };
- Console.WriteLine(route);
- }
- Console.WriteLine($"Ready for world tour! Planned stops: {route}");
- }
- private static string RemoveStop(string route, int startIndex, int endIndex)
- {
- if (IsIndexValid(route.Length, startIndex) && IsIndexValid(route.Length, endIndex))
- {
- route = route.Remove(startIndex, endIndex - startIndex + 1);
- }
- return route;
- }
- private static string AddStop(string route, int startIndex, string newStop)
- {
- if (IsIndexValid(route.Length, startIndex))
- {
- route = route.Insert(startIndex, newStop);
- }
- return route;
- }
- private static string ReplaceStops(string route, string oldStop, string newStop)
- {
- return route.Replace(oldStop, newStop);
- }
- private static bool IsIndexValid(int length, int index)
- {
- return index >= 0 && index < length;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement