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 CommandInterpreter
- {
- class CommandInterpreter
- {
- public static List<string> collection;
- static void Main(string[] args)
- {
- collection = Console.ReadLine().
- Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
- string inputLine = Console.ReadLine();
- while (!inputLine.Equals("end"))
- {
- string[] commandInfo =
- inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
- if (commandInfo.Length == 5)
- {
- if ((int.Parse(commandInfo[2]) < 0 || int.Parse(commandInfo[2]) >= collection.Count) ||
- (int.Parse(commandInfo[4]) < 0 ||
- (int.Parse(commandInfo[2]) + int.Parse(commandInfo[4]) > collection.Count)))
- {
- Console.WriteLine("Invalid input parameters.");
- inputLine = Console.ReadLine();
- continue;
- }
- if (commandInfo[0] == "reverse")
- {
- ReverseTheCollection(commandInfo);
- }
- else if (commandInfo[0] == "sort")
- {
- SortTheCollection(commandInfo);
- }
- }
- else
- {
- if (int.Parse(commandInfo[1]) < 0)
- {
- Console.WriteLine("Invalid input parameters.");
- inputLine = Console.ReadLine();
- continue;
- }
- if (commandInfo[0] == "rollLeft")
- {
- RollCollectionToTheLeft(commandInfo);
- }
- else if (commandInfo[0] == "rollRight")
- {
- RollCollectionToTheRight(commandInfo);
- }
- }
- inputLine = Console.ReadLine();
- }
- Console.WriteLine($"[{string.Join(", ", collection)}]");
- }
- private static void RollCollectionToTheRight(string[] commandInfo)
- {
- int rollTimes = int.Parse(commandInfo[1]);
- for (int currRotation = 0; currRotation < rollTimes; currRotation++)
- {
- string elementHolder = collection[collection.Count - 1];
- collection.RemoveAt(collection.Count - 1);
- collection.Insert(0, elementHolder);
- }
- }
- private static void RollCollectionToTheLeft(string[] commandInfo)
- {
- int rollTimes = int.Parse(commandInfo[1]);
- for (int currRotation = 0; currRotation < rollTimes; currRotation++)
- {
- string elementHolder = collection[0];
- collection.RemoveAt(0);
- collection.Insert(collection.Count, elementHolder);
- }
- }
- private static void SortTheCollection(string[] commandInfo)
- {
- int startPos = int.Parse(commandInfo[2]);
- int count = int.Parse(commandInfo[4]);
- int endPos = int.Parse(commandInfo[2]) + int.Parse(commandInfo[4]);
- List<string> sortedSection = new List<string>();
- for (int currElement = startPos; currElement < endPos; currElement++)
- {
- sortedSection.Add(collection[currElement]);
- }
- sortedSection = sortedSection.OrderBy(x => x).ToList();
- collection.RemoveRange(startPos, count);
- collection.InsertRange(startPos, sortedSection);
- }
- private static void ReverseTheCollection(string[] commandInfo)
- {
- int startPos = int.Parse(commandInfo[2]);
- int count = int.Parse(commandInfo[4]);
- int endPos = int.Parse(commandInfo[2]) + int.Parse(commandInfo[4]);
- List<string> reversedSection = new List<string>();
- for (int currElement = endPos - 1; currElement >= startPos; currElement--)
- {
- reversedSection.Add(collection[currElement]);
- }
- collection.RemoveRange(startPos, count);
- collection.InsertRange(startPos, reversedSection);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment