Advertisement
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(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries).ToList();
- while (true) {
- string command = Console.ReadLine();
- if (command== "end") {
- break;
- }
- long start = 0;
- long count = 0;
- string[] commands = command.Split();
- string instruction = commands[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)//za6to e start > seq.Count-1 ,a ne start > seq.Count
- {
- Console.WriteLine("Invalid input parameters.");
- }
- else
- {
- seq = ReverseAndSort(seq, start, count, instruction);
- }
- }
- else {
- count = int.Parse(commands[1]);
- if (count < 0)
- {
- Console.WriteLine("Invalid input parameters.");
- }
- else {
- seq = Roll(seq,count,instruction);
- }
- }
- }
- Console.WriteLine($"[{String.Join(", ", seq)}]");
- }
- private static List<string> Roll(List<string> seq, long count, string instruction)
- {
- count = count % seq.Count;
- for (int i = 0; i < count; i++)
- {
- List<string> take = new List<string>();
- List<string> skip = new List<string>();
- if (instruction == "rollLeft")
- {
- take = seq.Take(1).ToList();
- skip = seq.Skip(1).ToList();
- }
- else {
- take = seq.Take(seq.Count()-1).ToList();
- skip = seq.Skip(take.Count()).ToList();
- }
- List<string> temp = new List<string>();
- temp.AddRange(skip);
- temp.AddRange(take);
- seq = temp;
- }
- return seq;
- }
- private static List<string> ReverseAndSort(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> temp = new List<string>();
- temp.AddRange(leftPart);
- temp.AddRange(toTreat);
- temp.AddRange(rightPart);
- return temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement