Advertisement
Guest User

Untitled

a guest
Jun 1st, 2015
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. class P01CommandInterpreter
  6. {
  7.     static void Main()
  8.     {
  9.         var numbers = Regex.Split(Console.ReadLine(), @"\s+").ToList();
  10.  
  11.         string command = Console.ReadLine();
  12.         while (!command.ToLower().Equals("end"))
  13.         {
  14.             int startIndex = 0;
  15.             int endIndex = 0;
  16.             int count = 0;
  17.             var commandSplit = Regex.Split(command, @"\s+");
  18.             if (commandSplit.Length > 3)
  19.             {
  20.                 startIndex = int.Parse(commandSplit[2]);
  21.                 endIndex = int.Parse(commandSplit[4]) + startIndex;
  22.             }
  23.             else
  24.             {
  25.                 count = int.Parse(commandSplit[1]);
  26.                 if (count > int.MaxValue)
  27.                 {
  28.                     count = int.MaxValue;
  29.                 }
  30.             }
  31.  
  32.             if ((startIndex < 0 || startIndex > numbers.Count - 1) || (endIndex < 0 || endIndex > numbers.Count) || count < 0)
  33.             {
  34.                 Console.WriteLine("Invalid input parameters.");
  35.             }
  36.             else
  37.             {
  38.                 switch (commandSplit[0])
  39.                 {
  40.                     case "reverse":
  41.                         Reverse(numbers, startIndex, endIndex);
  42.                         break;
  43.                     case "sort":
  44.                         Sort(numbers, startIndex, endIndex);
  45.                         break;
  46.                     case "rollLeft":
  47.                         if (count > numbers.Count)
  48.                         {
  49.                             count = count % numbers.Count;
  50.                         }
  51.                         RollLeft(numbers, count);
  52.                         break;
  53.                     case "rollRight":
  54.                         if (count > numbers.Count)
  55.                         {
  56.                             count = count % numbers.Count;
  57.                         }
  58.                         RollRight(numbers, count);
  59.                         break;
  60.                 }
  61.             }
  62.             command = Console.ReadLine();
  63.         }
  64.  
  65.         Console.WriteLine(string.Format("[" + string.Join(", ", numbers) + "]"));
  66.     }
  67.  
  68.     static List<string> Reverse(List<string> numbers, int startIndex, int endIndex)
  69.     {
  70.         List<string> sublist1 = new List<string>();
  71.         List<string> sublist2 = new List<string>();
  72.         List<string> sublist3 = new List<string>();
  73.         for (int i = 0; i < numbers.Count; i++)
  74.         {
  75.             if (i < startIndex)
  76.             {
  77.                 sublist1.Add(numbers[i]);
  78.             }
  79.             else if (i >= startIndex && i < endIndex)
  80.             {
  81.                 sublist2.Add(numbers[i]);
  82.             }
  83.             else
  84.             {
  85.                 sublist3.Add(numbers[i]);
  86.             }
  87.         }
  88.         sublist2.Reverse();
  89.         numbers.Clear();
  90.         numbers.AddRange(sublist1);
  91.         numbers.AddRange(sublist2);
  92.         numbers.AddRange(sublist3);
  93.  
  94.         return numbers;
  95.     }
  96.  
  97.     static List<string> Sort(List<string> numbers, int startIndex, int endIndex)
  98.     {
  99.         List<string> sublist1 = new List<string>();
  100.         List<string> sublist2 = new List<string>();
  101.         List<string> sublist3 = new List<string>();
  102.         for (int i = 0; i < numbers.Count; i++)
  103.         {
  104.             if (i < startIndex)
  105.             {
  106.                 sublist1.Add(numbers[i]);
  107.             }
  108.             else if (i >= startIndex && i < endIndex)
  109.             {
  110.                 sublist2.Add(numbers[i]);
  111.             }
  112.             else
  113.             {
  114.                 sublist3.Add(numbers[i]);
  115.             }
  116.         }
  117.         sublist2.Sort();
  118.         numbers.Clear();
  119.         numbers.AddRange(sublist1);
  120.         numbers.AddRange(sublist2);
  121.         numbers.AddRange(sublist3);
  122.  
  123.         return numbers;
  124.     }
  125.  
  126.     static List<string> RollLeft(List<string> numbers, int count)
  127.     {
  128.         while (count > 0)
  129.         {
  130.             string temp = numbers[0];
  131.             numbers.RemoveAt(0);
  132.             numbers.Add(temp);
  133.             count--;
  134.         }
  135.  
  136.         return numbers;
  137.     }
  138.  
  139.     static List<string> RollRight(List<string> numbers, int count)
  140.     {
  141.         while (count > 0)
  142.         {
  143.             string temp = numbers[numbers.Count - 1];
  144.             numbers.RemoveAt(numbers.Count - 1);
  145.             numbers.Insert(0, temp);
  146.             count--;
  147.         }
  148.  
  149.         return numbers;
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement