-Annie-

CommandInterpreter

Jun 14th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. namespace CommandInterpreter
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class CommandInterpreter
  8.     {
  9.         public static void Main()
  10.         {
  11.             List<string> collection = Console.ReadLine()
  12.                 .Split(new[] { ' ', '\t'}, StringSplitOptions.RemoveEmptyEntries)
  13.                 .ToList();
  14.  
  15.             string command = Console.ReadLine();
  16.  
  17.             while (command != "end")
  18.             {
  19.                 string[] commandArguments = command.Split(' '); ;
  20.  
  21.                 switch (commandArguments[0])
  22.                 {
  23.                     case "reverse":
  24.                         ExecuteReverseCommand(collection, commandArguments);
  25.                         break;
  26.                     case "sort":
  27.                         ExecuteSortCommand(collection, commandArguments);
  28.                         break;
  29.                     case "rollLeft":
  30.                         ExecuteRollLeftCommand(collection, commandArguments);
  31.                         break;
  32.                     case "rollRight":
  33.                         ExecuteRollRightComamnd(collection, commandArguments);
  34.                         break;
  35.                 }
  36.  
  37.                 command = Console.ReadLine();
  38.             }
  39.  
  40.             Console.WriteLine("[{0}]", string.Join(", ", collection));
  41.         }
  42.  
  43.         private static void ExecuteRollRightComamnd(List<string> collection, string[] commandArguments)
  44.         {
  45.             int count = int.Parse(commandArguments[1]) % collection.Count;
  46.  
  47.             if (count < 0)
  48.             {
  49.                 Console.WriteLine("Invalid input parameters.");
  50.                 return;
  51.             }
  52.  
  53.             for (int i = 0; i < count; i++)
  54.             {
  55.                 string lastElement = collection[collection.Count-1];
  56.                 collection.RemoveAt(collection.Count - 1);
  57.                 collection.Insert(0, lastElement);
  58.             }
  59.         }
  60.  
  61.         private static void ExecuteRollLeftCommand(List<string> collection, string[] commandArguments)
  62.         {
  63.             int count = int.Parse(commandArguments[1]) % collection.Count;
  64.  
  65.             if (count < 0)
  66.             {
  67.                 Console.WriteLine("Invalid input parameters.");
  68.                 return;
  69.             }
  70.  
  71.             for (int i = 0; i < count; i++)
  72.             {
  73.                 string fisrtElement = collection[0];
  74.                 collection.Remove(fisrtElement);
  75.                 collection.Add(fisrtElement);
  76.             }
  77.         }
  78.  
  79.         private static void ExecuteSortCommand(List<string> collection, string[] commandArguments)
  80.         {
  81.             int start = int.Parse(commandArguments[2]);
  82.             int count = int.Parse(commandArguments[4]);
  83.  
  84.             if (start < 0 || start >= collection.Count || count < 0 || start + count > collection.Count)
  85.             {
  86.                 Console.WriteLine("Invalid input parameters.");
  87.                 return;
  88.             }
  89.  
  90.             collection.Sort(start, count, StringComparer.InvariantCulture);
  91.         }
  92.  
  93.         private static void ExecuteReverseCommand(List<string> collection, string[] commandArguments)
  94.         {
  95.             int start = int.Parse(commandArguments[2]);
  96.             int count = int.Parse(commandArguments[4]);
  97.  
  98.             if (start < 0 || start >= collection.Count || count < 0 || start + count > collection.Count)
  99.             {
  100.                 Console.WriteLine("Invalid input parameters.");
  101.                 return;
  102.             }
  103.  
  104.             collection.Reverse(start, count);
  105.         }
  106.     }
  107. }
  108.  
  109. // Example for input:
  110. //1 2 5 8 7 3 10 6 4 9
  111. //reverse from 2 count 4
  112. //end
Advertisement
Add Comment
Please, Sign In to add comment