Guest User

Untitled

a guest
Feb 22nd, 2017
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.78 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 Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<string> input = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  14.             string[] command = Console.ReadLine().Split(' ');
  15.             int start = 0;
  16.             int count = 0;
  17.             List<string> currList = new List<string>();
  18.  
  19.             while (command[0] != "end")
  20.             {
  21.                 switch (command[0])
  22.                 {
  23.                     case "reverse":
  24.                         start = int.Parse(command[2]);
  25.                         count = int.Parse(command[4]);
  26.  
  27.                         if (start < 0 || start >= input.Count || (start + count) > input.Count || count < 0)
  28.                         {
  29.                             Console.WriteLine("Invalid input parameters.");
  30.                             break;
  31.                         }
  32.  
  33.                         currList = input.Skip(start).Take(count).Reverse().ToList();
  34.                         input.RemoveRange(start, count); // Освобождаваме местата , на които ще вмъкнем новия List;
  35.                         input.InsertRange(start, currList);
  36.                         break;
  37.  
  38.                     case "sort":
  39.                         start = int.Parse(command[2]);
  40.                         count = int.Parse(command[4]);
  41.  
  42.                         if (start < 0 || (start + count) > input.Count || count < 0 || start >= input.Count)
  43.                         {
  44.                             Console.WriteLine("Invalid input parameters.");
  45.                             break;
  46.                         }
  47.  
  48.                         currList = input.Skip(start).Take(count).OrderBy(str => str).ToList(); // str е в скобите , за да помним , че това, което сортираме е string;
  49.                         input.RemoveRange(start, count); // Освобождаваме местата , на които ще вмъкнем новия List;
  50.                         input.InsertRange(start, currList);
  51.                         break;
  52.  
  53.                     case "rollLeft":
  54.                         count = int.Parse(command[1]);
  55.  
  56.                         if (count < 0)
  57.                         {
  58.                             Console.WriteLine("Invalid input parameters.");
  59.                             break;
  60.                         }
  61.  
  62.                         for (int i = 0; i < count % input.Count; i++)
  63.                         {
  64.                             string element = input[0];
  65.                             input.RemoveAt(0);
  66.                             input.Add(element);
  67.                         }
  68.  
  69.                         break;
  70.  
  71.                     case "rollRight":
  72.                         count = int.Parse(command[1]);
  73.  
  74.                         if (count < 0)
  75.                         {
  76.                             Console.WriteLine("Invalid input parameters.");
  77.                             break;
  78.                         }
  79.  
  80.                         for (int i = 0; i < count % input.Count; i++)
  81.                         {
  82.                             string element = input[input.Count - 1];
  83.                             input.RemoveAt(input.Count - 1);
  84.                             input.Insert(0, element);
  85.                         }
  86.  
  87.                         break;
  88.                     default:
  89.                         break;
  90.                 }
  91.  
  92.  
  93.                 command = Console.ReadLine().Split(' ');
  94.  
  95.             }
  96.  
  97.  
  98.  
  99.             string output = string.Join((", "), input);
  100.             Console.WriteLine($"[{output}]");
  101.  
  102.  
  103.         }
  104.     }
  105. }
Add Comment
Please, Sign In to add comment