Advertisement
sivancheva

ArrayManipulator

Sep 13th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 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_ArrayManipulator
  8. {
  9. class ArrayManipulator
  10. {
  11. static void Main(string[] args)
  12. {
  13. var inputArrayFirst = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  14.  
  15. var resultArr = new int[inputArrayFirst.Length]; //копие на входящия arr и после пази крайния резултат
  16. Array.Copy(inputArrayFirst, resultArr, inputArrayFirst.Length);
  17. var tempArr = new int[inputArrayFirst.Length];
  18.  
  19. var command = Console.ReadLine().Split(' ').Select(x=>x.Trim()).ToArray();
  20. var indexResult = 0;
  21.  
  22.  
  23. while (command[0] != "end")
  24. {
  25. if (command[0] == "exchange")
  26. {
  27. var index = int.Parse(command[1]);
  28.  
  29. if (index > resultArr.Length-1 || index < 0)
  30. {
  31. Console.WriteLine("Invalid index");
  32. command = Console.ReadLine().Split(' ').ToArray();
  33. continue;
  34. }
  35. Array.Copy(resultArr, index+1, tempArr, 0, (resultArr.Length-(index+1)));
  36. Array.Copy(resultArr, 0, tempArr, (resultArr.Length - (index + 1)), index+1);
  37. Array.Copy(tempArr, resultArr, resultArr.Length);
  38. // Console.WriteLine("[" + string.Join(", ", inputArray) + "]"); // позлвам да си проверя дали е ок резултата
  39. }
  40. if (command[0] == "max" || command[0] == "min")
  41. {
  42. bool allEqual = resultArr.Skip(1).All(s => int.Equals(resultArr[0], s)); // ако са еднакви елементите нямя мин/макс
  43. if (allEqual)
  44. {
  45. Console.WriteLine("No matches");
  46. command = Console.ReadLine().Split(' ').ToArray();
  47. continue;
  48. }
  49.  
  50. if (command[1] == "even")
  51. {
  52. bool allOdd = resultArr.All(x => x % 2 != 0);
  53.  
  54. if (allOdd)
  55. {
  56. Console.WriteLine("No matches");
  57. command = Console.ReadLine().Split(' ').ToArray();
  58. continue;
  59. }
  60. }
  61. if (command[1] == "odd")
  62. {
  63. bool allEven = resultArr.All(x => x % 2 == 0);
  64.  
  65. if (allEven)
  66. {
  67. Console.WriteLine("No matches");
  68. command = Console.ReadLine().Split(' ').ToArray();
  69. continue;
  70. }
  71. }
  72. if (command[0] == "max" && command[1] == "even")
  73. {
  74. indexResult = resultArr.Select(x => x).Where(x => x % 2 == 0).OrderByDescending(y => y).First();
  75. }
  76. else if (command[0] == "max" && command[1] == "odd")
  77. {
  78. indexResult = resultArr.Select(x => x).Where(x => x % 2 != 0).OrderByDescending(y => y).First();
  79.  
  80. }
  81. else if (command[0] == "min" && command[1] == "even")
  82. {
  83. indexResult = resultArr.Select(x => x).Where(x => x % 2 == 0).OrderByDescending(y => y).Last();
  84.  
  85. }
  86. else if (command[0] == "min" && command[1] == "odd")
  87. {
  88. indexResult = resultArr.Select(x => x).Where(x => x % 2 != 0).OrderByDescending(y => y).Last();
  89.  
  90. }
  91. int maxIndex = resultArr.ToList().LastIndexOf(indexResult);
  92. Console.WriteLine(maxIndex);
  93. }
  94. if (command[0] == "first" || command[0] == "last")
  95. {
  96. var resultList = new List<int>();
  97. var count = int.Parse(command[1]);
  98. bool allEqual = resultArr.Skip(1).All(s => int.Equals(resultArr[0], s));
  99.  
  100. if (count > resultArr.Length)
  101. {
  102. Console.WriteLine("Invalid count");
  103. command = Console.ReadLine().Split(' ').ToArray();
  104. continue;
  105. }
  106. if (command[0] == "first" && command[2] == "even")
  107. {
  108. resultList = resultArr.Select(x => x).Where(x => x % 2 == 0).Take(count).ToList();
  109. }
  110. else if (command[0] == "first" && command[2] == "odd")
  111. {
  112. resultList = resultArr.Select(x => x).Where(x => x % 2 != 0).Take(count).ToList();
  113. }
  114. else if (command[0] == "last" && command[2] == "even")
  115. {
  116. resultList = resultArr.Select(x => x).Where(x => x % 2 == 0).Reverse().Take(count).Reverse().ToList();
  117. }
  118. else if (command[0] == "last" && command[2] == "odd")
  119. {
  120. resultList = resultArr.Select(x => x).Where(x => x % 2 != 0).Reverse().Take(count).Reverse().ToList();
  121. }
  122. Console.WriteLine("[" + string.Join(", ", resultList) + "]");
  123. }
  124. command = Console.ReadLine().Split(' ').ToArray();
  125. }
  126. Console.WriteLine("[" + string.Join(", ", resultArr) + "]");
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement