Advertisement
sivancheva

CommandInterpreter2

Nov 4th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 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. namespace _02_CommandInterpreter2
  8. {
  9. class CommandInterpreter2
  10. {
  11. static void Main(string[] args)
  12. {
  13. var input = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  14.  
  15. var command = Console.ReadLine();
  16.  
  17. var manipulatedArr = new List<string>(input).ToList();
  18. while (true)
  19. {
  20.  
  21. if (command == "end")
  22. {
  23. break;
  24. }
  25. var commandArr = command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  26.  
  27. switch (commandArr[0])
  28. {
  29. case "reverse": ReversePartOfArr(manipulatedArr, int.Parse(commandArr[2]), int.Parse(commandArr[4]));
  30. break;
  31. case "sort": SortPartOfArr(manipulatedArr, int.Parse(commandArr[2]), int.Parse(commandArr[4]));
  32. break;
  33. case "rollLeft": RollLeft(manipulatedArr, int.Parse(commandArr[1]));
  34. break;
  35. case "rollRight": RollRight(manipulatedArr, int.Parse(commandArr[1]));
  36. break;
  37. default:
  38. break;
  39. }
  40.  
  41. command = Console.ReadLine();
  42. }
  43. Console.WriteLine("[" +String.Join(", ",manipulatedArr) +"]");
  44. }
  45.  
  46. public static void ReversePartOfArr(List<string> manipulatedArr,int index, int count)
  47. {
  48.  
  49. if (index + count > manipulatedArr.Count || index >= manipulatedArr.Count || index < 0 || count < 0 )
  50. {
  51. Console.WriteLine("Invalid input parameters.");
  52. }
  53. else
  54. {
  55. manipulatedArr.Reverse(index, count);
  56. }
  57. }
  58. private static void SortPartOfArr(List<string> manipulatedArr, int index, int count)
  59. {
  60. if (index >= manipulatedArr.Count || index <0 || count < 0 || index + count > manipulatedArr.Count )
  61. {
  62. Console.WriteLine("Invalid input parameters.");
  63. }
  64. else
  65. {
  66. manipulatedArr.Sort(index,count,null);
  67. }
  68. }
  69. private static void RollLeft(List<string> manipulatedArr, int count)
  70. {
  71. if (count < 0)
  72. {
  73. Console.WriteLine("Invalid input parameters.");
  74. }
  75. for (int i = 0; i < count%manipulatedArr.Count; i++)
  76. {
  77. manipulatedArr.Add(manipulatedArr[0]);
  78. manipulatedArr.RemoveAt(0);
  79. }
  80. }
  81. private static void RollRight(List<string> manipulatedArr, int count)
  82. {
  83. if (count < 0)
  84. {
  85. Console.WriteLine("Invalid input parameters.");
  86. }
  87. for (int i = 0; i < count % manipulatedArr.Count; i++)
  88. {
  89. manipulatedArr.Insert(0, manipulatedArr[manipulatedArr.Count-1]);
  90. manipulatedArr.RemoveAt(manipulatedArr.Count -1);
  91. }
  92. }
  93.  
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement