Advertisement
buena_girl

Array Manipulator

Jun 13th, 2019
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.84 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. namespace ArrayManipulator
  4. {
  5. class Program
  6. {
  7. static void ExchangeIndex(int[] inputArr, int index)
  8. {
  9. int[] newArr = new int[inputArr.Length];
  10. int[] cutInputArrFirst = new int[index + 1];
  11. int[] cutInputArrSecond = new int[inputArr.Length - index - 1];
  12. int counter = 0;
  13. for (int n = 0; n < inputArr.Length; n++)
  14. {
  15. if (n <= index)
  16. {
  17. cutInputArrFirst[n] = inputArr[n];
  18. }
  19. else
  20. {
  21. cutInputArrSecond[counter] = inputArr[n];
  22. counter++;
  23. }
  24. }
  25. Array.ConstrainedCopy(cutInputArrFirst, 0, newArr, newArr.Length - index - 1, cutInputArrFirst.Length);
  26. Array.ConstrainedCopy(cutInputArrSecond, 0, newArr, 0, cutInputArrSecond.Length);
  27. //Console.WriteLine("[" + string.Join(", ", newArr) + "]");
  28. Array.Copy(newArr, inputArr, newArr.Length);
  29. }
  30.  
  31. static void MaxEvenOdd(int[] arr, string evenOrOdd)
  32. {
  33. int maxValue = int.MinValue;
  34. int highestIndex = -1;
  35. for (int n = 0; n < arr.Length; n++)
  36. {
  37. if (arr[n] > maxValue)
  38. {
  39. if (evenOrOdd == "even" && arr[n] % 2 == 0)
  40. {
  41. maxValue = arr[n];
  42. highestIndex = n;
  43. }
  44. else if (evenOrOdd == "odd" && arr[n] % 2 != 0)
  45. {
  46. maxValue = arr[n];
  47. highestIndex = n;
  48. }
  49. }
  50. }
  51. if (highestIndex == -1)
  52. {
  53. Console.WriteLine("No matches");
  54. }
  55. else
  56. {
  57. Console.WriteLine(highestIndex);
  58. }
  59. }
  60.  
  61. static void MinEvenOdd(int[] arr, string evenOrOdd)
  62. {
  63. int minValue = int.MaxValue;
  64. int highesRightmosttIndex = -1;
  65. for (int n = arr.Length - 1; n >= 0; n--)
  66. {
  67. if (arr[n] < minValue)
  68. {
  69. if (evenOrOdd == "even" && arr[n] % 2 == 0)
  70. {
  71. minValue = arr[n];
  72. highesRightmosttIndex = n;
  73. }
  74. else if (evenOrOdd == "odd" && arr[n] % 2 != 0)
  75. {
  76. minValue = arr[n];
  77. highesRightmosttIndex = n;
  78. }
  79. }
  80. }
  81. if (highesRightmosttIndex == -1)
  82. {
  83. Console.WriteLine("No matches");
  84. }
  85. else
  86. {
  87. Console.WriteLine(highesRightmosttIndex);
  88. }
  89. }
  90.  
  91. static void PrintDefinedNumberOfEvenOrOddFirst(int[] arr, int number, string evenOrOdd)
  92. {
  93. if (number > arr.Length)
  94. {
  95. Console.WriteLine("Invalid count");
  96. return;
  97. }
  98. int counterEven = 0;
  99. int counterOdd = 0;
  100. for (int n = 0; n < arr.Length; n++)
  101. {
  102. if (arr[n] % 2 == 0)
  103. {
  104. counterEven++;
  105. }
  106. else
  107. {
  108. counterOdd++;
  109. }
  110. }
  111. if ((counterOdd == 0 && evenOrOdd == "odd") || (counterEven == 0 && evenOrOdd == "even"))
  112. {
  113. Console.WriteLine("[]");
  114. return;
  115. }
  116. if (counterEven < number && evenOrOdd == "even")
  117. {
  118. number = counterEven;
  119. }
  120. if (counterOdd < number && evenOrOdd == "odd")
  121. {
  122. number = counterOdd;
  123. }
  124. int[] arrOdd = new int[counterOdd];
  125. int[] arrEven = new int[counterEven];
  126. counterEven = 0;
  127. counterOdd = 0;
  128. for (int m = 0; m < arr.Length; m++)
  129. {
  130. if (arr[m] % 2 == 0)
  131. {
  132. arrEven[counterEven] = arr[m];
  133. counterEven++;
  134. }
  135. else
  136. {
  137. arrOdd[counterOdd] = arr[m];
  138. counterOdd++;
  139. }
  140. }
  141.  
  142. for (int r = 0; r < number; r++)
  143. {
  144. if (r == 0)
  145. {
  146. Console.Write("[");
  147. }
  148. if (evenOrOdd == "even")
  149. {
  150. Console.Write(arrEven[r] + ", ");
  151. }
  152. else if (evenOrOdd == "odd")
  153. {
  154. Console.Write(arrOdd[r]);
  155. if (r != number - 1)
  156. {
  157. Console.Write(", ");
  158. }
  159. }
  160. if (r == number - 1)
  161. {
  162. Console.Write("]");
  163. }
  164. }
  165. Console.WriteLine();
  166. }
  167.  
  168. static void PrintDefinedNumberOfEvenOrOddLast(int[] arr, int number, string evenOrOdd)
  169. {
  170. if (number > arr.Length)
  171. {
  172. Console.WriteLine("Invalid count");
  173. return;
  174. }
  175. int counterEven = 0;
  176. int counterOdd = 0;
  177. for (int n = 0; n < arr.Length; n++)
  178. {
  179. if (arr[n] % 2 == 0)
  180. {
  181. counterEven++;
  182. }
  183. else
  184. {
  185. counterOdd++;
  186. }
  187. }
  188. if ((counterOdd == 0 && evenOrOdd == "odd") || (counterEven == 0 && evenOrOdd == "even"))
  189. {
  190. Console.WriteLine("[]");
  191. return;
  192. }
  193. if (counterEven < number && evenOrOdd == "even")
  194. {
  195. number = counterEven;
  196. }
  197. if (counterOdd < number && evenOrOdd == "odd")
  198. {
  199. number = counterOdd;
  200. }
  201. int[] arrOdd = new int[counterOdd];
  202. int[] arrEven = new int[counterEven];
  203. counterEven = 0;
  204. counterOdd = 0;
  205. for (int m = arr.Length - 1; m >= 0; m--)
  206. {
  207. if (arr[m] % 2 == 0)
  208. {
  209. arrEven[counterEven] = arr[m];
  210. counterEven++;
  211. }
  212. else
  213. {
  214. arrOdd[counterOdd] = arr[m];
  215. counterOdd++;
  216. }
  217. }
  218. Array.Reverse(arrEven);
  219. Array.Reverse(arrOdd);
  220.  
  221. for (int r = 0; r < number; r++)
  222. {
  223. if (r == 0)
  224. {
  225. Console.Write("[");
  226. }
  227. if (evenOrOdd == "even")
  228. {
  229. Console.Write(arrEven[r] + ", ");
  230. }
  231. else if (evenOrOdd == "odd")
  232. {
  233. Console.Write(arrOdd[r]);
  234. if (r != number - 1)
  235. Console.Write(", ");
  236. }
  237. if (r == number - 1)
  238. {
  239. Console.Write("]");
  240. }
  241. }
  242. Console.WriteLine();
  243. }
  244. static void Main(string[] args)
  245. {
  246. int[] inputArr = Console.ReadLine()
  247. .Split()
  248. .Select(int.Parse)
  249. .ToArray();
  250.  
  251. while (true)
  252. {
  253. string action = Console.ReadLine();
  254.  
  255. if (action == "end")
  256. {
  257. Console.WriteLine("[" + string.Join(", ", inputArr) + "]");
  258. break;
  259. }
  260. else
  261. {
  262. if (action.Contains("exchange") == true)
  263. {
  264. string[] actionAsArr = action.Split();
  265. if (int.Parse(actionAsArr[1]) > inputArr.Length)
  266. {
  267. Console.WriteLine("Invalid index");
  268. }
  269. else
  270. {
  271. ExchangeIndex(inputArr, int.Parse(actionAsArr[1]));
  272. }
  273. }
  274. else if (action == "max even")
  275. {
  276. MaxEvenOdd(inputArr, "even");
  277. }
  278. else if (action == "max odd")
  279. {
  280. MaxEvenOdd(inputArr, "odd");
  281. }
  282. else if (action == "min even")
  283. {
  284. MinEvenOdd(inputArr, "even");
  285. }
  286. else if (action == "min odd")
  287. {
  288. MinEvenOdd(inputArr, "odd");
  289. }
  290. else if (action.Contains("first") == true)
  291. {
  292. string[] actionAsArr = action.Split();
  293. PrintDefinedNumberOfEvenOrOddFirst(inputArr, int.Parse(actionAsArr[1]), actionAsArr[2]);
  294. }
  295. else if (action.Contains("last") == true)
  296. {
  297. string[] actionAsArr = action.Split();
  298. PrintDefinedNumberOfEvenOrOddLast(inputArr, int.Parse(actionAsArr[1]), actionAsArr[2]);
  299. }
  300. }
  301. }
  302. }
  303. }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement