Advertisement
sivancheva

CommandInterpreter

Sep 4th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. namespace CommandInterpreter
  9. {
  10. class CommandInterpreter
  11. {
  12. static void Main(string[] args)
  13. {
  14.  
  15. var input = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  16. .Select(s => s.Trim()).ToArray();
  17.  
  18. var modifiedList = new List<string>(input).ToList();
  19. int lenght = modifiedList.Count - 1;
  20.  
  21. var command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  22. .Select(s => s.Trim()).ToArray();
  23.  
  24. while (command[0] != "end")
  25. {
  26.  
  27. if (command[0] == "reverse" || command[0] == "sort")
  28. {
  29. int start = int.Parse(command[2]);
  30. int count = int.Parse(command[4]);
  31. if ((start < 0) || (count < 1) || ((start + count) > input.Length) || (start > input.Length - 1)
  32. || (count > input.Length))
  33. {
  34. Console.WriteLine("Invalid input parameters.");
  35. goto END;
  36. }
  37.  
  38. if (command[0] == "reverse")
  39. {
  40. int a = 0;
  41. for (int i = start; i < (start + count); i++)
  42. {
  43.  
  44. modifiedList[i] = input[(count + start - 1) - a];
  45. a++;
  46. }
  47. }
  48.  
  49. else if (command[0] == "sort")
  50. {
  51. List<string> part = modifiedList.GetRange(start, count);
  52. part = part.OrderByDescending(a => a).ToList();
  53. modifiedList.RemoveRange(start, count);
  54.  
  55. int b = 0;
  56. for (int i = start; i < (start + count); i++)
  57. {
  58. modifiedList.Insert(start, part[b]);
  59. b++;
  60. }
  61.  
  62. }
  63.  
  64. }
  65. else if (command[0] == "rollLeft")
  66. {
  67. long countLeftRoll = long.Parse(command[1]);
  68.  
  69. for (long i = 0; i < countLeftRoll; i++)
  70. {
  71. modifiedList.Add(modifiedList[0]);
  72. modifiedList.RemoveAt(0);
  73.  
  74. }
  75. }
  76.  
  77. else if (command[0] == "rollRight")
  78. {
  79. long countRightRoll = long.Parse(command[1]);
  80.  
  81. for (long i = 0; i < countRightRoll; i++)
  82. {
  83. modifiedList.Insert(0, modifiedList[lenght]);
  84. modifiedList.RemoveAt(lenght + 1);
  85.  
  86. }
  87. }
  88. END:
  89. command = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  90. .Select(s => s.Trim()).ToArray();
  91. }
  92.  
  93. Console.WriteLine("[" + String.Join(", ", modifiedList) + "]");
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement