Advertisement
NastySwipy

Prog. Fundamentals Exam Prep III - 02. Command Interpreter

Jun 25th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Numerics;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace _14_ExamPreparation3
  9. {
  10.     class Program
  11.     {
  12.       static void Main(string[] args)
  13.         {
  14.             string[] array = Console.ReadLine()
  15.                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  16.  
  17.             while (true)
  18.             {
  19.                 string line = Console.ReadLine();
  20.                 if (line == "end")
  21.                 {
  22.                     break;
  23.                 }
  24.                 string[] commandParts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  25.                 switch (commandParts[0])
  26.                 {
  27.                     case "reverse":
  28.                         array = ReverseArrayPart(array, int.Parse(commandParts[2]), int.Parse(commandParts.Last()));
  29.                         break;
  30.                     case "sort":
  31.                         array = SortArrayPart(array, int.Parse(commandParts[2]), int.Parse(commandParts.Last()));
  32.                         break;
  33.                     case "rollLeft":
  34.                         if (int.Parse(commandParts[1]) < 0)
  35.                         {
  36.                             Console.WriteLine("Invalid input parameters.");
  37.                             break;
  38.                         }
  39.                         array = Roll(array, int.Parse(commandParts[1]), "left");
  40.                         break;
  41.                     case "rollRight":
  42.                         if (int.Parse(commandParts[1]) < 0)
  43.                         {
  44.                             Console.WriteLine("Invalid input parameters.");
  45.                             break;
  46.                         }
  47.                         array = Roll(array, int.Parse(commandParts[1]), "right");
  48.                         break;
  49.                 }
  50.             }
  51.             Console.WriteLine($"[{string.Join(", ", array)}]");
  52.         }
  53.  
  54.         static string[] Roll(string[] array, int count, string side)
  55.         {
  56.             List<string> list = array.ToList();
  57.             if (side == "left")
  58.             {
  59.                 for (int i = 0; i < count; i++)
  60.                 {
  61.                     string firsElement = list[0];
  62.                     list.RemoveAt(0);
  63.                     list.Insert(list.Count, firsElement);
  64.                 }
  65.             }
  66.             if (side == "right")
  67.             {
  68.                 for (int i = 0; i < count; i++)
  69.                 {
  70.                     string LastElement = list[list.Count - 1];
  71.                     list.RemoveAt(list.Count - 1);
  72.                     list.Insert(0, LastElement);
  73.                 }
  74.             }
  75.             array = list.ToArray();
  76.             list.Clear();
  77.             return array;
  78.         }
  79.  
  80.         static string[] ReverseArrayPart(string[] array, int start, int count)
  81.         {
  82.             if (start < 0 || start >= array.Length)
  83.             {
  84.                 Console.WriteLine("Invalid input parameters.");
  85.                 return array;
  86.             }
  87.             if (start + count < 0 || count < 0 || start + count - 1>= array.Length)
  88.             {
  89.                 Console.WriteLine("Invalid input parameters.");
  90.                 return array;
  91.             }
  92.             string[] firstPart = array.Take(start).ToArray();
  93.             string[] reversedPart = array.Skip(start).Take(count).Reverse().ToArray();
  94.             string[] lastPart = array.Skip(start + count).ToArray();
  95.             return firstPart.Concat(reversedPart).Concat(lastPart).ToArray();
  96.         }
  97.  
  98.         static string[] SortArrayPart(string[] array, int start, int count)
  99.         {
  100.             if (start < 0 || start >= array.Length)
  101.             {
  102.                 Console.WriteLine("Invalid input parameters.");
  103.                 return array;
  104.             }
  105.             if (start + count < 0 || count < 0 || start + count - 1>= array.Length)
  106.             {
  107.                 Console.WriteLine("Invalid input parameters.");
  108.                 return array;
  109.             }
  110.             string[] firstPart = array.Take(start).ToArray();
  111.             string[] sortedPart = array.Skip(start).Take(count).OrderBy(s => s).ToArray();
  112.             string[] lastPart = array.Skip(start + count).ToArray();
  113.             return firstPart.Concat(sortedPart).Concat(lastPart).ToArray();
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement