kalinkata

Command Interpreter

Nov 4th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class CommandInterpreter
  7. {
  8.     static void Main()
  9.     {
  10.         string[] inputArray = Regex.Split(Console.ReadLine(), @"\s+").Where(x => x != String.Empty).ToArray();
  11.         string commandLine = Console.ReadLine();
  12.  
  13.         string firstStr = "";
  14.  
  15.         while (commandLine != "end")
  16.         {
  17.             string[] data = commandLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  18.  
  19.             firstStr = data[0];
  20.  
  21.             if (firstStr == "reverse")
  22.             {
  23.                 Match match = Regex.Match(commandLine, @"reverse from (.*) count (.*)");
  24.  
  25.                 if (match.Success)
  26.                 {
  27.                     try
  28.                     {
  29.                         uint start = uint.Parse(match.Groups[1].Value.ToString());
  30.                         uint end = uint.Parse(match.Groups[2].Value.ToString());
  31.  
  32.                         inputArray = ReverseRange(inputArray, start, end);
  33.                     }
  34.                     catch (Exception)
  35.                     {
  36.                         Console.WriteLine("Invalid input parameters.");
  37.                     }
  38.                 }
  39.             }
  40.             else if (firstStr == "sort")
  41.             {
  42.                 Match match = Regex.Match(commandLine, @"sort from (.*) count (.*)");
  43.  
  44.                 if (match.Success)
  45.                 {
  46.                     try
  47.                     {
  48.                         uint start = uint.Parse(match.Groups[1].Value.ToString());
  49.                         uint end = uint.Parse(match.Groups[2].Value.ToString());
  50.  
  51.                         inputArray = SortRange(inputArray, start, end);
  52.                     }
  53.                     catch (Exception)
  54.                     {
  55.                         Console.WriteLine("Invalid input parameters.");
  56.                     }
  57.                 }
  58.             }
  59.             else if (firstStr == "rollLeft")
  60.             {
  61.                 Match match = Regex.Match(commandLine, @"rollLeft (.*) times");
  62.  
  63.                 if (match.Success)
  64.                 {
  65.                     try
  66.                     {
  67.                         uint count = uint.Parse(match.Groups[1].Value.ToString());
  68.  
  69.                         inputArray = RollLeft(inputArray, count);
  70.                     }
  71.                     catch (Exception)
  72.                     {
  73.                         Console.WriteLine("Invalid input parameters.");
  74.                     }
  75.                 }
  76.             }
  77.             else if (firstStr == "rollRight")
  78.             {
  79.                 Match match = Regex.Match(commandLine, @"rollRight (.*) times");
  80.  
  81.                 if (match.Success)
  82.                 {
  83.                     try
  84.                     {
  85.                         uint count = uint.Parse(match.Groups[1].Value.ToString());
  86.  
  87.                         inputArray = RollRight(inputArray, count);
  88.                     }
  89.                     catch (Exception)
  90.                     {
  91.                         Console.WriteLine("Invalid input parameters.");
  92.                     }
  93.                 }
  94.             }
  95.  
  96.             commandLine = Console.ReadLine();
  97.         }
  98.  
  99.         Console.WriteLine("[" + String.Join(", ", inputArray) + "]");
  100.     }
  101.  
  102.     private static string[] RollRight(string[] inputArray, uint count)
  103.     {
  104.         List<string> list = inputArray.ToList();
  105.  
  106.         for (int i = 0; i < count; i++)
  107.         {
  108.             string lastElement = list[list.Count - 1];
  109.  
  110.             list.Insert(0, lastElement);
  111.  
  112.             list.RemoveAt(list.Count - 1);
  113.         }
  114.  
  115.         return list.ToArray();
  116.     }
  117.  
  118.     private static string[] RollLeft(string[] inputArray, uint count)
  119.     {
  120.         List<string> list = inputArray.ToList();
  121.  
  122.         for (int i = 0; i < count; i++)
  123.         {
  124.             string firstElement = list[0];
  125.  
  126.             list.Add(firstElement);
  127.  
  128.             list.RemoveAt(0);
  129.         }
  130.  
  131.         return list.ToArray();
  132.     }
  133.  
  134.     private static string[] SortRange(string[] arr, uint start, uint end)
  135.     {
  136.         List<string> sub = new List<string>();
  137.  
  138.         for (int i = (int)start; i < end + start; i++)
  139.         {
  140.             sub.Add(arr[i]);
  141.         }
  142.  
  143.         var sorted = sub.ToArray().OrderBy(x => x).ToList();
  144.  
  145.         //sub.Sort();
  146.  
  147.         for (int i = (int)start; i < end + start; i++)
  148.         {
  149.             arr[i] = sorted[i - (int)start].ToString();
  150.         }
  151.  
  152.         return arr;
  153.     }
  154.     private static string[] ReverseRange(string[] arr, uint start, uint end)
  155.     {
  156.         List<string> sub = new List<string>();
  157.  
  158.         for (int i = (int)start; i < end + start; i++)
  159.         {
  160.             sub.Add(arr[i]);
  161.         }
  162.  
  163.         for (int i = (int)start; i < end + start; i++)
  164.         {
  165.             arr[i] = sub[(int)(end + start) - i - 1];
  166.         }
  167.  
  168.         return arr;
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment