Advertisement
Guest User

Untitled

a guest
Jun 1st, 2015
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class Problem1
  7. {
  8.     static void Main()
  9.     {
  10.         List<string> strings = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  11.         string command = Console.ReadLine();
  12.         while (command != "end")
  13.         {
  14.             string[] splitCommands = command.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  15.             switch (splitCommands[0])
  16.             {
  17.                 case "reverse": Reverse(strings, int.Parse(splitCommands[2]), int.Parse(splitCommands[4])); break;
  18.                 case "sort": Sort(strings, int.Parse(splitCommands[2]), int.Parse(splitCommands[4])); break;
  19.                 case "rollLeft": RollLeft(strings, int.Parse(splitCommands[1])); break;
  20.                 case "rollRight": RollRight(strings, int.Parse(splitCommands[1])); break;
  21.                 default:
  22.                     break;
  23.             }
  24.             command = Console.ReadLine();
  25.         }
  26.         Console.Write("[{0}]", string.Join(", ", strings));
  27.     }
  28.  
  29.     private static void RollRight(List<string> strings, int p)
  30.     {
  31.         if (p < 0)
  32.         {
  33.             Console.WriteLine("Invalid input parameters.");
  34.             return;
  35.         }
  36.         int count = strings.Count;
  37.         int realRoll = p % count;
  38.         for (int i = 0; i < realRoll; i++)
  39.         {
  40.             strings.Insert(0, strings[count - 1]);
  41.             strings.RemoveAt(count);
  42.         }
  43.     }
  44.  
  45.     private static void RollLeft(List<string> strings, int p)
  46.     {
  47.         if (p < 0)
  48.         {
  49.             Console.WriteLine("Invalid input parameters.");
  50.             return;
  51.         }
  52.         int realRoll = p % strings.Count;
  53.         for (int i = 0; i < realRoll; i++)
  54.         {
  55.             strings.Add(strings[0]);
  56.             strings.RemoveAt(0);
  57.         }
  58.     }
  59.  
  60.     private static void Sort(List<string> strings, int p1, int p2)
  61.     {
  62.         int count = strings.Count;
  63.         if (p1 < 0 || p2 < 0 || p1 >= count || (long)p1 + (long)p2 - 1 >= count)
  64.         {
  65.             Console.WriteLine("Invalid input parameters.");
  66.             return;
  67.         }
  68.         strings.Sort(p1, p2, StringComparer.InvariantCulture);
  69.     }
  70.  
  71.     private static void Reverse(List<string> strings, int p1, int p2)
  72.     {
  73.         int count = strings.Count;
  74.         if (p1 < 0 || p2 < 0 || p1 >= count || (long)p1 + (long)p2 - 1 >= count)
  75.         {
  76.             Console.WriteLine("Invalid input parameters.");
  77.             return;
  78.         }
  79.         strings.Reverse(p1, p2);
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement