Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace CommandInterpreter
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class CommandInterpreter
- {
- public static void Main()
- {
- List<string> collection = Console.ReadLine()
- .Split(new[] { ' ', '\t'}, StringSplitOptions.RemoveEmptyEntries)
- .ToList();
- string command = Console.ReadLine();
- while (command != "end")
- {
- string[] commandArguments = command.Split(' '); ;
- switch (commandArguments[0])
- {
- case "reverse":
- ExecuteReverseCommand(collection, commandArguments);
- break;
- case "sort":
- ExecuteSortCommand(collection, commandArguments);
- break;
- case "rollLeft":
- ExecuteRollLeftCommand(collection, commandArguments);
- break;
- case "rollRight":
- ExecuteRollRightComamnd(collection, commandArguments);
- break;
- }
- command = Console.ReadLine();
- }
- Console.WriteLine("[{0}]", string.Join(", ", collection));
- }
- private static void ExecuteRollRightComamnd(List<string> collection, string[] commandArguments)
- {
- int count = int.Parse(commandArguments[1]) % collection.Count;
- if (count < 0)
- {
- Console.WriteLine("Invalid input parameters.");
- return;
- }
- for (int i = 0; i < count; i++)
- {
- string lastElement = collection[collection.Count-1];
- collection.RemoveAt(collection.Count - 1);
- collection.Insert(0, lastElement);
- }
- }
- private static void ExecuteRollLeftCommand(List<string> collection, string[] commandArguments)
- {
- int count = int.Parse(commandArguments[1]) % collection.Count;
- if (count < 0)
- {
- Console.WriteLine("Invalid input parameters.");
- return;
- }
- for (int i = 0; i < count; i++)
- {
- string fisrtElement = collection[0];
- collection.Remove(fisrtElement);
- collection.Add(fisrtElement);
- }
- }
- private static void ExecuteSortCommand(List<string> collection, string[] commandArguments)
- {
- int start = int.Parse(commandArguments[2]);
- int count = int.Parse(commandArguments[4]);
- if (start < 0 || start >= collection.Count || count < 0 || start + count > collection.Count)
- {
- Console.WriteLine("Invalid input parameters.");
- return;
- }
- collection.Sort(start, count, StringComparer.InvariantCulture);
- }
- private static void ExecuteReverseCommand(List<string> collection, string[] commandArguments)
- {
- int start = int.Parse(commandArguments[2]);
- int count = int.Parse(commandArguments[4]);
- if (start < 0 || start >= collection.Count || count < 0 || start + count > collection.Count)
- {
- Console.WriteLine("Invalid input parameters.");
- return;
- }
- collection.Reverse(start, count);
- }
- }
- }
- // Example for input:
- //1 2 5 8 7 3 10 6 4 9
- //reverse from 2 count 4
- //end
Advertisement
Add Comment
Please, Sign In to add comment