Advertisement
sivancheva

ArrayManipulatorFinal

Sep 13th, 2017
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 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];
  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.  
  43. if (command[1] == "even")
  44. {
  45. bool allOdd = resultArr.All(x => x % 2 != 0);
  46.  
  47. if (allOdd)
  48. {
  49. Console.WriteLine("No matches");
  50. command = Console.ReadLine().Split(' ').ToArray();
  51. continue;
  52. }
  53. }
  54. if (command[1] == "odd")
  55. {
  56. bool allEven = resultArr.All(x => x % 2 == 0);
  57.  
  58. if (allEven)
  59. {
  60. Console.WriteLine("No matches");
  61. command = Console.ReadLine().Split(' ').ToArray();
  62. continue;
  63. }
  64. }
  65. if (command[0] == "max" && command[1] == "even")
  66. {
  67. indexResult = resultArr.Select(x => x).Where(x => x % 2 == 0).OrderByDescending(y => y).First();
  68. }
  69. else if (command[0] == "max" && command[1] == "odd")
  70. {
  71. indexResult = resultArr.Select(x => x).Where(x => x % 2 != 0).OrderByDescending(y => y).First();
  72.  
  73. }
  74. else if (command[0] == "min" && command[1] == "even")
  75. {
  76. indexResult = resultArr.Select(x => x).Where(x => x % 2 == 0).OrderByDescending(y => y).Last();
  77.  
  78. }
  79. else if (command[0] == "min" && command[1] == "odd")
  80. {
  81. indexResult = resultArr.Select(x => x).Where(x => x % 2 != 0).OrderByDescending(y => y).Last();
  82.  
  83. }
  84. int maxIndex = resultArr.ToList().LastIndexOf(indexResult);
  85. Console.WriteLine(maxIndex);
  86. }
  87. if (command[0] == "first" || command[0] == "last")
  88. {
  89. var resultList = new List<int>();
  90. var count = int.Parse(command[1]);
  91. bool allEqual = resultArr.Skip(1).All(s => int.Equals(resultArr[0], s));
  92.  
  93. if (count > resultArr.Length)
  94. {
  95. Console.WriteLine("Invalid count");
  96. command = Console.ReadLine().Split(' ').ToArray();
  97. continue;
  98. }
  99. if (command[0] == "first" && command[2] == "even")
  100. {
  101. resultList = resultArr.Select(x => x).Where(x => x % 2 == 0).Take(count).ToList();
  102. }
  103. else if (command[0] == "first" && command[2] == "odd")
  104. {
  105. resultList = resultArr.Select(x => x).Where(x => x % 2 != 0).Take(count).ToList();
  106. }
  107. else if (command[0] == "last" && command[2] == "even")
  108. {
  109. resultList = resultArr.Select(x => x).Where(x => x % 2 == 0).Reverse().Take(count).Reverse().ToList();
  110. }
  111. else if (command[0] == "last" && command[2] == "odd")
  112. {
  113. resultList = resultArr.Select(x => x).Where(x => x % 2 != 0).Reverse().Take(count).Reverse().ToList();
  114. }
  115. Console.WriteLine("[" + string.Join(", ", resultList) + "]");
  116. }
  117. command = Console.ReadLine().Split(' ').ToArray();
  118. }
  119. Console.WriteLine("[" + string.Join(", ", resultArr) + "]");
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement