Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _9.CommandInterpreter
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> input = Console.ReadLine().Split(new char[] { ' '},StringSplitOptions.RemoveEmptyEntries).ToList();
- string[] command = Console.ReadLine().Split(' ').ToArray();
- while (command[0] != "end")
- {
- if (command[0]=="reverse")
- {
- int start = int.Parse(command[2]);
- int count = int.Parse(command[4]);
- if(IsValidCommand(input,start,count))
- {
- List<string> portion = new List<string>();
- for (int i = start; i < start + count; i++)
- {
- portion.Add(input[i]);
- }
- portion.Reverse();
- input.RemoveRange(start, count);
- input.InsertRange(start, portion);
- }
- else
- {
- Console.WriteLine("Invalid input parameters.");
- }
- }
- else if(command[0]=="sort")
- {
- int start = int.Parse(command[2]);
- int count = int.Parse(command[4]);
- if (IsValidCommand(input, start, count))
- {
- List<string> portion = new List<string>();
- for (int i = start; i < start + count; i++)
- {
- portion.Add(input[i]);
- }
- portion.Sort();
- input.RemoveRange(start, count);
- input.InsertRange(start, portion);
- }
- else
- {
- Console.WriteLine("Invalid input parameters.");
- }
- }
- else if(command[0] == "rollLeft")
- {
- int times = int.Parse(command[1]);
- if (times < 1)
- {
- Console.WriteLine("Invalid input parameters.");
- }
- else
- {
- for (int i = 0; i < times; i++)
- {
- List<string> inner = new List<string>();
- string start = input[0];
- for (int j = 1; j < input.Count; j++)
- {
- inner.Add(input[j]);
- }
- inner.Add(start);
- input = inner;
- }
- }
- }
- else if(command[0]== "rollRight")
- {
- int times = int.Parse(command[1]);
- if(times<1)
- {
- Console.WriteLine("Invalid input parameters.");
- }
- else
- {
- for (int i = 0; i < times; i++)
- {
- List<string> inner = new List<string>();
- string end = input[input.Count - 1];
- inner.Insert(0, end);
- for (int j = 0; j < input.Count - 1; j++)
- {
- inner.Add(input[j]);
- }
- input = inner;
- }
- }
- }
- else
- {
- Console.WriteLine("Invalid input parameters.");
- }
- command = Console.ReadLine().Split(' ').ToArray();
- }
- string separator = ", ";
- Console.Write("[");
- Console.Write(string.Join(separator,input));
- Console.WriteLine("]");
- }
- static bool IsValidCommand(List<string> input, int start, int count)
- {
- bool result = true;
- if((start<0)||(start>=input.Count-1))
- {
- result = false;
- }
- if((count<0)||(count+start>=input.Count-1))
- {
- result = false;
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment