vstoyanov

Untitled

Oct 30th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 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 _02.Command_Interpreter
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<string> inputElement = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  14.  
  15.             List<string> tempList = new List<string>();
  16.  
  17.             List<string> commands = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  18.  
  19.  
  20.             while (commands[0] != "end")
  21.             {
  22.                 if (commands[0] == "reverse")
  23.                 {
  24.                     int startIndex = int.Parse(commands[2]);
  25.                     int countElement = int.Parse(commands[4]);
  26.  
  27.                     if (startIndex < 0 || countElement < 0 || startIndex + countElement > inputElement.Count - 1)
  28.                     {
  29.                         Console.WriteLine("Invalid input parameters.");
  30.  
  31.                     }
  32.                     else
  33.                     {
  34.                         for (int i = startIndex; i < startIndex + countElement; i++)
  35.                         {
  36.                             tempList.Add(inputElement[i]);
  37.  
  38.  
  39.  
  40.                         }
  41.  
  42.                         tempList.Reverse();
  43.                         for (int i = 0; i < countElement; i++)
  44.                         {
  45.                             inputElement[i + startIndex] = tempList[i];
  46.                         }
  47.  
  48.                     }
  49.  
  50.  
  51.                 }
  52.                 else if (commands[0] == "rollLeft")
  53.                 {
  54.                     int count = int.Parse(commands[1]);
  55.  
  56.                     if (count >= 0)
  57.                     {
  58.                         for (int i = 0; i < count; i++)
  59.                         {
  60.                             inputElement.Add(inputElement[0]);
  61.                             inputElement.RemoveAt(0);
  62.  
  63.  
  64.                         }
  65.                     }
  66.                     else
  67.                     {
  68.                         Console.WriteLine("Invalid input parameters.");
  69.  
  70.                     }
  71.                 }
  72.                 else if (commands[0] == "rollRight")
  73.                 {
  74.                     int count = int.Parse(commands[1]);
  75.  
  76.                     if (count >= 0)
  77.                     {
  78.                         for (int i = 0; i < count; i++)
  79.                         {
  80.                             inputElement.Insert(0, inputElement[inputElement.Count - 1]);
  81.                             inputElement.RemoveAt(inputElement.Count - 1);
  82.  
  83.                         }
  84.                     }
  85.                     else
  86.                     {
  87.                         Console.WriteLine("Invalid input parameters.");
  88.  
  89.                     }
  90.                 }
  91.                 else if (commands[0] == "sort")
  92.                 {
  93.                     tempList = new List<string>();
  94.  
  95.  
  96.  
  97.                     int index = int.Parse(commands[2]);
  98.                     int count = int.Parse(commands[4]);
  99.  
  100.                     if (index < 0 || count < 0 || index + count > inputElement.Count - 1)
  101.                     {
  102.                         Console.WriteLine("Invalid input parameters.");
  103.  
  104.                     }
  105.                     else
  106.                     {
  107.                         for (int i = index; i < index + count; i++)
  108.                         {
  109.                             tempList.Add(inputElement[i]);
  110.                         }
  111.  
  112.  
  113.  
  114.                         tempList.Sort();
  115.  
  116.                         for (int i = 0; i < count; i++)
  117.                         {
  118.                             inputElement[i + index] = tempList[i];
  119.                         }
  120.                     }
  121.  
  122.                 }
  123.  
  124.  
  125.  
  126.                 commands = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  127.             }
  128.  
  129.  
  130.             Console.WriteLine("[" + string.Join(", ", inputElement) + "]");
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment