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 ConsoleApplication22
- {
- class Program
- {
- static string[] input;
- static void Main(string[] args)
- {
- input = Console.ReadLine().Split(' ').ToArray();
- string command = Console.ReadLine();
- while (command != "end")
- {
- string[] tokens = command.Split(' ');
- switch (tokens[0])
- {
- case "reverse":
- int start = int.Parse(tokens[2]);
- int count = int.Parse(tokens[4]);
- Reverse(start, count);
- break;
- case "sort":
- int starts = int.Parse(tokens[2]);
- int counts = int.Parse(tokens[4]);
- Sort(starts, counts);
- break;
- case "rollLeft":
- int numOfRotationLeft = int.Parse(tokens[1]);
- RollLeft(numOfRotationLeft);
- break;
- case "rollRight":
- int numOfRotationRight = int.Parse(tokens[1]);
- RollRight(numOfRotationRight);
- break;
- }
- command = Console.ReadLine();
- }
- }
- private static void RollLeft(int numOfRotationLeft)
- {
- for (int i = 0; i < numOfRotationLeft % input.Length; i++)
- {
- var temp = input[0];
- for (int j = 1; j < input.Length; j++)
- {
- input[j - 1] = input[j];
- }
- input[input.Length - 1] = temp;
- }
- Console.WriteLine("[" + string.Join(", ", input) + "]");
- }
- private static void RollRight(int numOfRotationRight)
- {
- for (int i = 0; i < numOfRotationRight; i++)
- {
- var temp = input[input.Length - 1];
- for (int j = input.Length - 1; j >= 1; j--)
- {
- input[j] = input[j - 1];
- }
- input[0] = temp;
- }
- Console.WriteLine("[" + string.Join(", ", input) + "]");
- }
- private static void Sort(int start, int count)
- {
- if (IsValid(start, count))
- {
- string[] str = input.Skip(start).Take(count).ToArray();
- Array.Sort(str);
- var num = new List<string>();
- for (int i = 0; i < input.Length - (input.Length - start); i++)
- num.Add(input[i]);
- num.AddRange(str);
- for (int i = start + count; i < input.Length; i++)
- num.Add(input[i]);
- Console.WriteLine("[" + string.Join(", ", num) + "]");
- }
- }
- private static void Reverse(int start, int count)
- {
- if (IsValid(start, count))
- {
- string[] str = input.Skip(start).Take(count).ToArray();
- Array.Reverse(str);
- var num = new List<string>();
- for (int i = 0; i < input.Length - (input.Length - start); i++)
- num.Add(input[i]);
- num.AddRange(str);
- for (int i = start + count; i < input.Length; i++)
- num.Add(input[i]);
- Console.WriteLine("[" + string.Join(", ", num) + "]");
- }
- }
- private static bool IsValid(int start, int count)
- {
- bool isInRange = start < input.Length && start >= 0;
- bool isValidCount = (start + count) <= input.Length && count >= 0;
- if (isInRange && isValidCount)
- {
- return true;
- }
- else
- {
- Console.WriteLine("Invalid input parameters.");
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment