Advertisement
bullit3189

Array Manipulator

Jan 23rd, 2019
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _11ArrayManipulator
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.  
  12. while (true)
  13. {
  14. string input = Console.ReadLine();
  15.  
  16. if (input== "end")
  17. {
  18. break;
  19. }
  20.  
  21. string[] command = input.Split();
  22.  
  23. if (command[0] == "exchange")
  24. {
  25. int positionToExchange = int.Parse(command[1]);
  26.  
  27. if (positionToExchange>=0 && positionToExchange<array.Length)
  28. {
  29.  
  30. Exchange(array, positionToExchange);
  31. }
  32. else
  33. {
  34. Console.WriteLine("Invalid index");
  35. continue;
  36. }
  37.  
  38. }
  39.  
  40. else if (command[0] == "max")
  41. {
  42. string typeNum = command[1];
  43. int index = -1;
  44.  
  45. if (typeNum=="odd")
  46. {
  47. index = MaxEvenOrOddIndex(array,1);
  48. Console.WriteLine(index);
  49. }
  50. else
  51. {
  52. index = MaxEvenOrOddIndex(array,0);
  53. Console.WriteLine(index);
  54. }
  55.  
  56. if (index==-1)
  57. {
  58. Console.WriteLine("No matches");
  59. continue;
  60. }
  61. }
  62.  
  63. else if (command[0]== "min")
  64. {
  65. string typeNum = command[1];
  66. int index = -1;
  67.  
  68. if (typeNum == "odd")
  69. {
  70. index=MinEvenOrOddIndex(array, 1);
  71.  
  72. }
  73. else
  74. {
  75. index = MinEvenOrOddIndex(array, 0);
  76.  
  77. }
  78. if (index == -1)
  79. {
  80. Console.WriteLine("No matches");
  81. continue;
  82. }
  83. Console.WriteLine(index);
  84. }
  85.  
  86. else if (command[0]=="first")
  87. {
  88. int count = int.Parse(command[1]);
  89.  
  90. if (count>array.Length)
  91. {
  92. Console.WriteLine("Invalid count");
  93. continue;
  94. }
  95. string typeNum = command[2];
  96. int[] result = new int[0];
  97.  
  98. if (typeNum=="odd")
  99. {
  100. result = FirstEvenOrOddElements(array, count, 1);
  101. }
  102. else if (typeNum=="even")
  103. {
  104. result = FirstEvenOrOddElements(array, count, 0);
  105. }
  106.  
  107. Console.WriteLine("[" + string.Join(", ", result) + "]");
  108. }
  109.  
  110. else if (command[0]=="last")
  111. {
  112. int count = int.Parse(command[1]);
  113.  
  114. if (count > array.Length)
  115. {
  116. Console.WriteLine("Invalid count");
  117. continue;
  118. }
  119.  
  120. string typeNum = command[2];
  121. int[] result = new int[0];
  122.  
  123. if (typeNum == "odd")
  124. {
  125. result = LastEvenOrOddElements(array, count, 1);
  126. }
  127. else if (typeNum == "even")
  128. {
  129. result = LastEvenOrOddElements(array, count, 0);
  130. }
  131.  
  132. Console.WriteLine("["+string.Join(", ", result)+"]");
  133. }
  134.  
  135. }
  136.  
  137. Console.WriteLine("[" + string.Join(", ", array) + "]");
  138. }
  139.  
  140. private static int[] LastEvenOrOddElements(int[] arr, int neededCount, int divisibleResult)
  141. {
  142. int[] resultArr = new int[neededCount];
  143. int currCounter = 0;
  144.  
  145. for (int i = arr.Length - 1; i >= 0; i--)
  146. {
  147. if (arr[i]%2==divisibleResult && currCounter<neededCount)
  148. {
  149. resultArr[currCounter] = arr[i];
  150. currCounter++;
  151. }
  152. }
  153.  
  154. if (currCounter>=neededCount)
  155. {
  156. return resultArr.Reverse().ToArray();
  157. }
  158. else
  159. {
  160. int[] tempArr = new int[currCounter];
  161. Array.Copy(resultArr, tempArr, currCounter);
  162. return tempArr.Reverse().ToArray();
  163. }
  164. }
  165.  
  166. public static void Exchange (int[] arr, int position)
  167. {
  168. for (int i = 0; i <= position; i++)
  169. {
  170. int firstNum = arr[0];
  171.  
  172. for (int j = 0; j < arr.Length-1; j++)
  173. {
  174. arr[j] = arr[j + 1];
  175. }
  176.  
  177. arr[arr.Length-1] = firstNum;
  178. }
  179.  
  180. }
  181.  
  182. public static int MaxEvenOrOddIndex (int[] arr, int divisonResult)
  183. {
  184. int maxNum = int.MinValue;
  185. int index = -1;
  186.  
  187. for (int i = 0; i < arr.Length; i++)
  188. {
  189. if (arr[i]>=maxNum && arr[i]%2==divisonResult)
  190. {
  191. maxNum = arr[i];
  192. index = i;
  193. }
  194. }
  195. return index;
  196. }
  197.  
  198. public static int MinEvenOrOddIndex (int[] arr, int divisonResult)
  199. {
  200. int minNum = int.MaxValue;
  201. int index = -1;
  202.  
  203. for (int i = 0; i < arr.Length; i++)
  204. {
  205. if (arr[i]<=minNum && arr[i]%2==divisonResult)
  206. {
  207. minNum = arr[i];
  208. index = i;
  209. }
  210. }
  211. return index;
  212. }
  213.  
  214. public static int[] FirstEvenOrOddElements (int[] arr, int neededCount, int divisibleResult)
  215. {
  216. int[] resultArr = new int[neededCount];
  217. int currCounter = 0;
  218.  
  219.  
  220. for (int i = 0; i < arr.Length; i++)
  221. {
  222. if (arr[i]%2==divisibleResult && currCounter<neededCount)
  223. {
  224. resultArr[currCounter] = arr[i];
  225. currCounter++;
  226. }
  227. }
  228.  
  229. if (currCounter>=neededCount)
  230. {
  231. return resultArr;
  232. }
  233. else
  234. {
  235. int[] tempArr = new int[currCounter];
  236. Array.Copy(resultArr, tempArr, currCounter);
  237. return tempArr;
  238. }
  239. }
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement