Advertisement
kiro671

Untitled

Jul 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 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)//za6to e start > seq.Count-1 ,a ne start > seq.Count
  33. {
  34. Console.WriteLine("Invalid input parameters.");
  35. }
  36. else
  37. {
  38. seq = ReverseAndSort(seq, start, count, instruction);
  39.  
  40. }
  41.  
  42. }
  43. else {
  44. count = int.Parse(commands[1]);
  45. if (count < 0)
  46. {
  47. Console.WriteLine("Invalid input parameters.");
  48. }
  49. else {
  50. seq = Roll(seq,count,instruction);
  51.  
  52. }
  53.  
  54.  
  55.  
  56. }
  57.  
  58.  
  59.  
  60.  
  61. }
  62.  
  63. Console.WriteLine($"[{String.Join(", ", seq)}]");
  64.  
  65. }
  66.  
  67. private static List<string> Roll(List<string> seq, long count, string instruction)
  68. {
  69. count = count % seq.Count;
  70. for (int i = 0; i < count; i++)
  71. {
  72. List<string> take = new List<string>();
  73. List<string> skip = new List<string>();
  74.  
  75. if (instruction == "rollLeft")
  76. {
  77. take = seq.Take(1).ToList();
  78. skip = seq.Skip(1).ToList();
  79. }
  80. else {
  81. take = seq.Take(seq.Count()-1).ToList();
  82. skip = seq.Skip(take.Count()).ToList();
  83. }
  84. List<string> temp = new List<string>();
  85. temp.AddRange(skip);
  86. temp.AddRange(take);
  87. seq = temp;
  88.  
  89.  
  90. }
  91. return seq;
  92. }
  93.  
  94. private static List<string> ReverseAndSort(List<string> seq, long start, long count, string instruction)
  95. {
  96.  
  97. List<string> leftPart = seq.Take((int)start).ToList();
  98. List<string> rightPart = seq.Skip((int)(start + count)).ToList();
  99. List<string> toTreat = seq.Skip((int)start).Take((int)count).ToList();
  100.  
  101.  
  102. if (instruction == "reverse")
  103. {
  104. toTreat.Reverse();
  105. }
  106. else if(instruction == "sort"){
  107. toTreat.Sort();
  108. }
  109.  
  110. List<string> temp = new List<string>();
  111.  
  112. temp.AddRange(leftPart);
  113. temp.AddRange(toTreat);
  114. temp.AddRange(rightPart);
  115. return temp;
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement