Advertisement
YavorJS

Command Interpreter

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