Advertisement
kiro671

Untitled

Jul 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Collections.Generic;
  5.  
  6. namespace CommandInterpreter
  7.  
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<string> seq = Console.ReadLine().Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries).ToList();
  14.  
  15.  
  16.  
  17.  
  18. while (true) {
  19. string command = Console.ReadLine();
  20. if (command== "end") {
  21. break;
  22. }
  23.  
  24. long start = 0;
  25. long count = 0;
  26. string[] commands = command.Split();
  27. string instruction = commands[0];
  28. if (instruction == "reverse" || instruction == "sort")
  29. {
  30. start = long.Parse(commands[2]);
  31. count = long.Parse(commands[4]);
  32. if (start < 0 || start + count > seq.Count || count < 0 || start > seq.Count-1)
  33. {
  34. ////////////////////////////za6to e start > seq.Count-1 ,a ne start > seq.Count
  35. Console.WriteLine("Invalid input parameters.");
  36. }
  37. else
  38. {
  39. seq = ReverseAndSort(seq, start, count, instruction);
  40.  
  41. }
  42.  
  43. }
  44. else {
  45. count = int.Parse(commands[1]);
  46. if (count < 0)
  47. {
  48. Console.WriteLine("Invalid input parameters.");
  49. }
  50. else {
  51. seq = Roll(seq,count,instruction);
  52.  
  53. }
  54.  
  55.  
  56.  
  57. }
  58.  
  59.  
  60.  
  61.  
  62. }
  63.  
  64. Console.WriteLine($"[{String.Join(", ", seq)}]");
  65.  
  66. }
  67.  
  68. private static List<string> Roll(List<string> seq, long count, string instruction)
  69. {
  70. count = count % seq.Count;
  71. for (int i = 0; i < count; i++)
  72. {
  73. List<string> take = new List<string>();
  74. List<string> skip = new List<string>();
  75.  
  76. if (instruction == "rollLeft")
  77. {
  78. take = seq.Take(1).ToList();
  79. skip = seq.Skip(1).ToList();
  80. }
  81. else {
  82. take = seq.Take(seq.Count()-1).ToList();
  83. skip = seq.Skip(take.Count()).ToList();
  84. }
  85. List<string> temp = new List<string>();
  86. temp.AddRange(skip);
  87. temp.AddRange(take);
  88. seq = temp;
  89.  
  90.  
  91. }
  92. return seq;
  93. }
  94.  
  95. private static List<string> ReverseAndSort(List<string> seq, long start, long count, string instruction)
  96. {
  97.  
  98. List<string> leftPart = seq.Take((int)start).ToList();
  99. List<string> rightPart = seq.Skip((int)(start + count)).ToList();
  100. List<string> toTreat = seq.Skip((int)start).Take((int)count).ToList();
  101.  
  102.  
  103. if (instruction == "reverse")
  104. {
  105. toTreat.Reverse();
  106. }
  107. else if(instruction == "sort"){
  108. toTreat.Sort();
  109. }
  110.  
  111. List<string> temp = new List<string>();
  112.  
  113. temp.AddRange(leftPart);
  114. temp.AddRange(toTreat);
  115. temp.AddRange(rightPart);
  116. return temp;
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement