simonradev

CommandInterpreter

Dec 8th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CommandInterpreter
  8. {
  9.     class CommandInterpreter
  10.     {
  11.         public static List<string> collection;
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             collection = Console.ReadLine().
  16.                 Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  17.  
  18.             string inputLine = Console.ReadLine();
  19.             while (!inputLine.Equals("end"))
  20.             {
  21.                 string[] commandInfo =
  22.                     inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  23.  
  24.                 if (commandInfo.Length == 5)
  25.                 {
  26.                     if ((int.Parse(commandInfo[2]) < 0 || int.Parse(commandInfo[2]) >= collection.Count) ||
  27.                         (int.Parse(commandInfo[4]) < 0 ||
  28.                         (int.Parse(commandInfo[2]) + int.Parse(commandInfo[4]) > collection.Count)))
  29.                     {
  30.                         Console.WriteLine("Invalid input parameters.");
  31.                         inputLine = Console.ReadLine();
  32.                         continue;
  33.                     }
  34.  
  35.                     if (commandInfo[0] == "reverse")
  36.                     {
  37.                        ReverseTheCollection(commandInfo);
  38.                     }
  39.                     else if (commandInfo[0] == "sort")
  40.                     {
  41.                         SortTheCollection(commandInfo);
  42.                     }
  43.                 }
  44.                 else
  45.                 {
  46.                     if (int.Parse(commandInfo[1]) < 0)
  47.                     {
  48.                         Console.WriteLine("Invalid input parameters.");
  49.                         inputLine = Console.ReadLine();
  50.                         continue;
  51.                     }
  52.  
  53.                     if (commandInfo[0] == "rollLeft")
  54.                     {
  55.                         RollCollectionToTheLeft(commandInfo);
  56.                     }
  57.                     else if (commandInfo[0] == "rollRight")
  58.                     {
  59.                         RollCollectionToTheRight(commandInfo);
  60.                     }
  61.                 }
  62.  
  63.                 inputLine = Console.ReadLine();
  64.             }
  65.  
  66.             Console.WriteLine($"[{string.Join(", ", collection)}]");
  67.         }
  68.  
  69.         private static void RollCollectionToTheRight(string[] commandInfo)
  70.         {
  71.             int rollTimes = int.Parse(commandInfo[1]);
  72.  
  73.             for (int currRotation = 0; currRotation < rollTimes; currRotation++)
  74.             {
  75.                 string elementHolder = collection[collection.Count - 1];
  76.                 collection.RemoveAt(collection.Count - 1);
  77.                 collection.Insert(0, elementHolder);
  78.             }
  79.         }
  80.  
  81.         private static void RollCollectionToTheLeft(string[] commandInfo)
  82.         {
  83.             int rollTimes = int.Parse(commandInfo[1]);
  84.  
  85.             for (int currRotation = 0; currRotation < rollTimes; currRotation++)
  86.             {
  87.                 string elementHolder = collection[0];
  88.                 collection.RemoveAt(0);
  89.                 collection.Insert(collection.Count, elementHolder);
  90.             }
  91.         }
  92.  
  93.         private static void SortTheCollection(string[] commandInfo)
  94.         {
  95.             int startPos = int.Parse(commandInfo[2]);
  96.             int count = int.Parse(commandInfo[4]);
  97.             int endPos = int.Parse(commandInfo[2]) + int.Parse(commandInfo[4]);
  98.  
  99.             List<string> sortedSection = new List<string>();
  100.  
  101.             for (int currElement = startPos; currElement < endPos; currElement++)
  102.             {
  103.                 sortedSection.Add(collection[currElement]);
  104.             }
  105.  
  106.             sortedSection = sortedSection.OrderBy(x => x).ToList();
  107.  
  108.             collection.RemoveRange(startPos, count);
  109.             collection.InsertRange(startPos, sortedSection);
  110.         }
  111.  
  112.         private static void ReverseTheCollection(string[] commandInfo)
  113.         {
  114.             int startPos = int.Parse(commandInfo[2]);
  115.             int count = int.Parse(commandInfo[4]);
  116.             int endPos = int.Parse(commandInfo[2]) + int.Parse(commandInfo[4]);
  117.  
  118.             List<string> reversedSection = new List<string>();
  119.  
  120.             for (int currElement = endPos - 1; currElement >= startPos; currElement--)
  121.             {
  122.                 reversedSection.Add(collection[currElement]);
  123.             }
  124.  
  125.             collection.RemoveRange(startPos, count);
  126.             collection.InsertRange(startPos, reversedSection);
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment