Advertisement
Guest User

Array Manipuator

a guest
Oct 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 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()
  11. .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  12. .Select(int.Parse)
  13. .ToArray();
  14. string[] command = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  15.  
  16. while(command[0]!="end")
  17. {
  18. if (command[0] == "exchange")
  19. {
  20. int number = int.Parse(command[1]);
  21. if(number<array.Length && number>=0)
  22. {
  23.  
  24. Exchange(number, array);
  25. }
  26. else
  27. {
  28. Console.WriteLine("Invalid index");
  29. }
  30.  
  31. }
  32.  
  33. else if (command[0] == "max" || command[0] == "min")
  34. {
  35. if(command[0] == "max")
  36. {
  37. MaxOddOrMaxEven(command[1], array);
  38. }
  39.  
  40. else if (command[0] == "min")
  41. {
  42. MinOddOrMinEven(command[1], array);
  43. }
  44. }
  45.  
  46. else if (command[0] == "first" || command[0] == "last")
  47. {
  48. int count = int.Parse(command[1]);
  49. if(count>array.Length || count < 0)
  50. {
  51. Console.WriteLine("Invalid count");
  52. }
  53. else
  54. {
  55. if (command[0] == "first")
  56. {
  57. FirstOddOrFirstEven(command[1], command[2], array);
  58. }
  59.  
  60. else if (command[0] == "last")
  61. {
  62. LastOddOrLastEven(command[1], command[2], array);
  63. }
  64. }
  65. }
  66.  
  67. command = Console.ReadLine().Split();
  68. }
  69.  
  70. Console.WriteLine($"[{string.Join(", ", array)}]");
  71. }
  72.  
  73. public static void Exchange(int number, int[] array)
  74. {
  75. for (int i = 0; i <= number; i++)
  76. {
  77. for (int j = 0; j < array.Length - 1; j++)
  78. {
  79. int firstNum = array[j];
  80. int indexOfReverseCell = j + 1;
  81. array[j] = array[indexOfReverseCell];
  82. array[indexOfReverseCell] = firstNum;
  83. }
  84. }
  85. }
  86.  
  87. public static void MaxOddOrMaxEven(string type, int[] arr)
  88. {
  89. int index = -1;
  90. int maxNumber = int.MinValue;
  91. if(type=="odd")
  92. {
  93. for (int i = 0; i < arr.Length; i++)
  94. {
  95. if(arr[i]>=maxNumber && arr[i]%2==1)
  96. {
  97. index = i;
  98. maxNumber = arr[i];
  99. }
  100. }
  101. }
  102. else if(type == "even")
  103. {
  104. for (int i = 0; i < arr.Length; i++)
  105. {
  106. if (arr[i] >= maxNumber && arr[i] % 2 == 0)
  107. {
  108. index = i;
  109. maxNumber = arr[i];
  110. }
  111. }
  112. }
  113. if(index == -1 )
  114. {
  115. Console.WriteLine("No matches");
  116. }
  117. else
  118. {
  119. Console.WriteLine(index);
  120. }
  121.  
  122. }
  123.  
  124. public static void MinOddOrMinEven(string type, int[] arr)
  125. {
  126. int index = -1;
  127. int minNumber = int.MaxValue;
  128. if (type == "odd")
  129. {
  130. for (int i = 0; i < arr.Length; i++)
  131. {
  132. if (arr[i] <= minNumber && arr[i] % 2 == 1)
  133. {
  134. index = i;
  135. minNumber = arr[i];
  136. }
  137. }
  138. }
  139. else if (type == "even")
  140. {
  141. for (int i = 0; i < arr.Length; i++)
  142. {
  143. if (arr[i] <= minNumber && arr[i] % 2 == 0)
  144. {
  145. index = i;
  146. minNumber = arr[i];
  147. }
  148. }
  149. }
  150.  
  151. if (index == -1)
  152. {
  153. Console.WriteLine("No matches");
  154. }
  155. else
  156. {
  157. Console.WriteLine(index);
  158. }
  159. }
  160.  
  161. public static void FirstOddOrFirstEven(string count,string type, int[] arr)
  162. {
  163. int num = int.Parse(count);
  164. int i = 0;
  165. int[] arrayFirstNums = new int[num];
  166.  
  167. if (type=="odd")
  168. {
  169. for (int j = 0; j < arr.Length; j++)
  170. {
  171. if(arr[j] % 2 == 1)
  172. {
  173. arrayFirstNums[i] = arr[j];
  174. i++;
  175. }
  176.  
  177. if(i >= num)
  178. {
  179. break;
  180. }
  181. }
  182. }
  183. else if(type == "even")
  184. {
  185. for (int j = 0; j < arr.Length; j++)
  186. {
  187. if (arr[j] % 2 == 0)
  188. {
  189. arrayFirstNums[i] = arr[j];
  190. i++;
  191. }
  192.  
  193. if (i >= num)
  194. {
  195. break;
  196. }
  197. }
  198. }
  199.  
  200. PrintFirstLast(i, arrayFirstNums);
  201. }
  202.  
  203. public static void LastOddOrLastEven(string count, string type, int[] arr)
  204. {
  205. int num = int.Parse(count);
  206. int i = 0;
  207. int[] arrayFirstNums = new int[num];
  208.  
  209. if (type == "odd")
  210. {
  211. for (int j = 0; j < arr.Length; j++)
  212. {
  213. if (arr[j] % 2 == 1)
  214. {
  215. arrayFirstNums[i] = arr[j];
  216. i++;
  217. }
  218.  
  219. if (i >= num)
  220. {
  221. break;
  222. }
  223. }
  224. }
  225. else if (type == "even")
  226. {
  227. for (int j = 0; j < arr.Length; j++)
  228. {
  229. if (arr[j] % 2 == 0)
  230. {
  231. arrayFirstNums[i] = arr[j];
  232. i++;
  233. }
  234.  
  235. if (i >= num)
  236. {
  237. break;
  238. }
  239. }
  240. }
  241.  
  242. PrintFirstLast(i, arrayFirstNums);
  243. }
  244.  
  245. public static void PrintFirstLast(int i, int[] arrayFirstNums)
  246. {
  247. if (i == 0)
  248. { Console.WriteLine("[]"); }
  249. else
  250. {
  251. int[] resultArr = new int[i];
  252. for (int x = 0; x < i; x++)
  253. {
  254. resultArr[x] = arrayFirstNums[x];
  255. }
  256.  
  257. Console.WriteLine($"[{string.Join(", ", resultArr)}]");
  258. }
  259. }
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement