Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CommandInterpreter
  6. {
  7. public class CommandInterpreter
  8. {
  9. public static void Main(string[] args)
  10. {
  11. List<string> input = Console.ReadLine()
  12. .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  13. .ToList();
  14.  
  15. string[] command = Console.ReadLine().Split();
  16. int start = 0;
  17. int count = 0;
  18. List<string> currList = new List<string>();
  19.  
  20. while (!command[0].Equals("end"))
  21. {
  22. switch (command[0])
  23. {
  24. case "reverse":
  25. start = int.Parse(command[2]);
  26. count = int.Parse(command[4]);
  27.  
  28. if (start < 0
  29. || count < 0
  30. || start >= input.Count
  31. || start + count > input.Count)
  32. {
  33. Console.WriteLine("Invalid input parameters.");
  34. break;
  35. }
  36. currList = input
  37. .Skip(start)
  38. .Take(count)
  39. .Reverse()
  40. .ToList();
  41.  
  42. input.RemoveRange(start, count);
  43. input.InsertRange(start, currList);
  44. break;
  45.  
  46. case "sort":
  47. start = int.Parse(command[2]);
  48. count = int.Parse(command[4]);
  49.  
  50. if (start < 0
  51. || count < 0
  52. || start >= input.Count
  53. || start + count > input.Count)
  54. {
  55. Console.WriteLine("Invalid input parameters.");
  56. break;
  57. }
  58. currList = input
  59. .Skip(start)
  60. .Take(count)
  61. .OrderBy(str => str)
  62. .ToList();
  63.  
  64. input.RemoveRange(start, count);
  65. input.InsertRange(start, currList);
  66. break;
  67.  
  68. case "rollLeft":
  69. count = int.Parse(command[1]);
  70.  
  71. if (count < 0)
  72. {
  73. Console.WriteLine("Invalid input parameters.");
  74. break;
  75. }
  76.  
  77. for (int i = 0; i < (count % input.Count); i++)
  78. {
  79. string element = input[0];
  80.  
  81. input.RemoveAt(0);
  82. input.Add(element);
  83. }
  84. break;
  85.  
  86. case "rollRight":
  87. count = int.Parse(command[1]);
  88.  
  89. if (count < 0)
  90. {
  91. Console.WriteLine("Invalid input parameters.");
  92. break;
  93. }
  94.  
  95. for (int i = 0; i < (count % input.Count); i++)
  96. {
  97. string element = input[input.Count - 1];
  98.  
  99. input.RemoveAt(input.Count - 1);
  100. input.Insert(0, element);
  101. }
  102. break;
  103.  
  104. default:
  105. break;
  106. }
  107.  
  108. command = Console.ReadLine().Split();
  109. }
  110. string output = string.Join(", ", input);
  111. Console.WriteLine($"[{output}]");
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement