Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using System.Collections.Generic;
- namespace CommandInterpreter
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> seq = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToList();
- while (true)
- {
- var command = Console.ReadLine();
- if (command == "end")
- {
- break;
- }
- string[] commands = command.Split();
- string instruction = commands[0];
- long start = 0;
- long count = 0;
- if (instruction == "reverse" || instruction == "sort")
- {
- start = long.Parse(commands[2]);
- count = long.Parse(commands[4]);
- if (start < 0 || start + count > seq.Count || count < 0 || start > seq.Count - 1)
- {
- Console.WriteLine("Invalid input parameters.");
- }
- else
- {
- seq = Reverse(seq, start, count, instruction);
- }
- }
- }
- Console.WriteLine($"[{String.Join(", ", seq)}]");
- }
- private static List<string> Reverse(List<string> seq, long start, long count, string instruction)
- {
- List<string> leftPart = seq.Take((int)start).ToList();
- List<string> rightPart = seq.Skip((int)(start + count)).ToList();
- List<string> toTreat = seq.Skip((int)start).Take((int)count).ToList();
- if (instruction == "reverse")
- {
- toTreat.Reverse();
- }
- else if (instruction == "sort")
- {
- toTreat.Sort();
- }
- List<string> treated = new List<string>();
- treated.AddRange(leftPart);
- treated.AddRange(toTreat);
- treated.AddRange(rightPart);
- return treated;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment