Advertisement
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;
- using System.Threading.Tasks;
- namespace Task2
- {
- class CommandInterpreter
- {
- static void Main(string[] args)
- {
- string[] array = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
- List<string> list = array.ToList();
- string input = Console.ReadLine();
- while (input != "end")
- {
- string[] inputTokens = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
- if (inputTokens[0] == "reverse")
- {
- int from = int.Parse(inputTokens[2]);
- int count = int.Parse(inputTokens[4]);
- if (from < 0 || from >= array.Length || (from + count) > array.Length || count < 0)
- {
- Console.WriteLine("Invalid input parameters.");
- goto Enter;
- }
- else
- {
- list.Reverse(from, count);
- array = list.ToArray();
- }
- }
- if (inputTokens[0] == "sort")
- {
- int from = int.Parse(inputTokens[2]);
- int count = int.Parse(inputTokens[4]);
- if (from < 0 || from >= array.Length || (from + count) > array.Length || count < 0)
- {
- Console.WriteLine("Invalid input parameters.");
- goto Enter;
- }
- else
- {
- Array.Sort(array, from * 1, count * 1);
- list = array.ToList();
- }
- }
- if (inputTokens[0] == "rollLeft")
- {
- int cnt = int.Parse(inputTokens[1]);
- if (cnt > 0)
- {
- list = list.Skip(cnt).Concat(list.Take(cnt)).ToList();
- array = list.ToArray();
- }
- else
- {
- Console.WriteLine("Invalid input parameters.");
- goto Enter;
- }
- }
- if (inputTokens[0] == "rollRight")
- {
- int cnt = int.Parse(inputTokens[1]);
- if (cnt >= 0)
- {
- for (int i = 0; i < cnt % list.Count; i++)
- {
- string lastElement = list[list.Count - 1];
- for (int j = list.Count - 1; j > 0; j--)
- {
- list[j] = list[j - 1];
- }
- list[0] = lastElement;
- }
- }
- else
- {
- Console.WriteLine("Invalid input parameters.");
- goto Enter;
- }
- }
- Enter:
- input = Console.ReadLine();
- }
- Console.Write($"[{string.Join(", ", list)}]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement