Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- class CommandInterpreter
- {
- static void Main()
- {
- string[] inputArray = Regex.Split(Console.ReadLine(), @"\s+").Where(x => x != String.Empty).ToArray();
- string commandLine = Console.ReadLine();
- string firstStr = "";
- while (commandLine != "end")
- {
- string[] data = commandLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- firstStr = data[0];
- if (firstStr == "reverse")
- {
- Match match = Regex.Match(commandLine, @"reverse from (.*) count (.*)");
- if (match.Success)
- {
- try
- {
- uint start = uint.Parse(match.Groups[1].Value.ToString());
- uint end = uint.Parse(match.Groups[2].Value.ToString());
- inputArray = ReverseRange(inputArray, start, end);
- }
- catch (Exception)
- {
- Console.WriteLine("Invalid input parameters.");
- }
- }
- }
- else if (firstStr == "sort")
- {
- Match match = Regex.Match(commandLine, @"sort from (.*) count (.*)");
- if (match.Success)
- {
- try
- {
- uint start = uint.Parse(match.Groups[1].Value.ToString());
- uint end = uint.Parse(match.Groups[2].Value.ToString());
- inputArray = SortRange(inputArray, start, end);
- }
- catch (Exception)
- {
- Console.WriteLine("Invalid input parameters.");
- }
- }
- }
- else if (firstStr == "rollLeft")
- {
- Match match = Regex.Match(commandLine, @"rollLeft (.*) times");
- if (match.Success)
- {
- try
- {
- uint count = uint.Parse(match.Groups[1].Value.ToString());
- inputArray = RollLeft(inputArray, count);
- }
- catch (Exception)
- {
- Console.WriteLine("Invalid input parameters.");
- }
- }
- }
- else if (firstStr == "rollRight")
- {
- Match match = Regex.Match(commandLine, @"rollRight (.*) times");
- if (match.Success)
- {
- try
- {
- uint count = uint.Parse(match.Groups[1].Value.ToString());
- inputArray = RollRight(inputArray, count);
- }
- catch (Exception)
- {
- Console.WriteLine("Invalid input parameters.");
- }
- }
- }
- commandLine = Console.ReadLine();
- }
- Console.WriteLine("[" + String.Join(", ", inputArray) + "]");
- }
- private static string[] RollRight(string[] inputArray, uint count)
- {
- List<string> list = inputArray.ToList();
- for (int i = 0; i < count; i++)
- {
- string lastElement = list[list.Count - 1];
- list.Insert(0, lastElement);
- list.RemoveAt(list.Count - 1);
- }
- return list.ToArray();
- }
- private static string[] RollLeft(string[] inputArray, uint count)
- {
- List<string> list = inputArray.ToList();
- for (int i = 0; i < count; i++)
- {
- string firstElement = list[0];
- list.Add(firstElement);
- list.RemoveAt(0);
- }
- return list.ToArray();
- }
- private static string[] SortRange(string[] arr, uint start, uint end)
- {
- List<string> sub = new List<string>();
- for (int i = (int)start; i < end + start; i++)
- {
- sub.Add(arr[i]);
- }
- var sorted = sub.ToArray().OrderBy(x => x).ToList();
- //sub.Sort();
- for (int i = (int)start; i < end + start; i++)
- {
- arr[i] = sorted[i - (int)start].ToString();
- }
- return arr;
- }
- private static string[] ReverseRange(string[] arr, uint start, uint end)
- {
- List<string> sub = new List<string>();
- for (int i = (int)start; i < end + start; i++)
- {
- sub.Add(arr[i]);
- }
- for (int i = (int)start; i < end + start; i++)
- {
- arr[i] = sub[(int)(end + start) - i - 1];
- }
- return arr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment