Guest User

Untitled

a guest
Aug 7th, 2020
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.82 KB | None | 0 0
  1. namespace ArrayManipulator
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. class ArrayManipulatorStart
  8. {
  9. static void Main()
  10. {
  11. string[] input = Console.ReadLine().Split(" ");
  12. int[] numbers = Array.ConvertAll(input, int.Parse);
  13.  
  14. string command = "";
  15.  
  16. while ((command = Console.ReadLine()) != "end")
  17. {
  18. string[] instructions = command.Split(" ");
  19.  
  20. if (instructions[0] == "exchange")
  21. {
  22. int second = int.Parse(instructions[1]);
  23. if (second > numbers.Length - 1 || second < 0)
  24. {
  25. Console.WriteLine("Invalid index");
  26. }
  27. else
  28. {
  29. numbers = Exchange(numbers, second);
  30. }
  31.  
  32. }
  33. else if (instructions[0] == "max")
  34. {
  35. if (instructions[1] == "odd")
  36. {
  37. int result = MaxOdd(numbers);
  38. if (result < 0)
  39. {
  40. Console.WriteLine("No matches");
  41. }
  42. else
  43. {
  44. Console.WriteLine(result);
  45. }
  46. }
  47. else if (instructions[1] == "even")
  48. {
  49. int result = MaxEven(numbers);
  50. if (result < 0)
  51. {
  52. Console.WriteLine("No matches");
  53. }
  54. else
  55. {
  56. Console.WriteLine(result);
  57. }
  58. }
  59. }
  60. else if (instructions[0] == "min")
  61. {
  62. if (instructions[1] == "odd")
  63. {
  64. int result = MinOdd(numbers);
  65. if (result < 0)
  66. {
  67. Console.WriteLine("No matches");
  68. }
  69. else
  70. {
  71. Console.WriteLine(result);
  72. }
  73. }
  74. else if (instructions[1] == "even")
  75. {
  76. int result = MinEven(numbers);
  77. if (result < 0)
  78. {
  79. Console.WriteLine("No matches");
  80. }
  81. else
  82. {
  83. Console.WriteLine(result);
  84. }
  85. }
  86. }
  87. else if (instructions[0] == "first")
  88. {
  89. int secondInstruction = int.Parse(instructions[1]);
  90. if (secondInstruction > numbers.Length)
  91. {
  92. Console.WriteLine("Invalid count");
  93. continue;
  94. }
  95. else if (instructions[2] == "odd")
  96. {
  97. List<int> temp = FirstOdds(numbers, secondInstruction);
  98. if (temp.Count <= secondInstruction)
  99. {
  100. Console.WriteLine("[" + string.Join(", ", temp) + "]");
  101. }
  102. else if (temp.Count > secondInstruction)
  103. {
  104. temp.RemoveRange(secondInstruction, temp.Count - secondInstruction);
  105. Console.WriteLine("[" + string.Join(", ", temp) + "]");
  106. }
  107. }
  108. else if (instructions[2] == "even")
  109. {
  110. List<int> temp = FirstEvens(numbers, secondInstruction);
  111. if (temp.Count <= secondInstruction)
  112. {
  113. Console.WriteLine("[" + string.Join(", ", temp) + "]");
  114. }
  115. else if (temp.Count > secondInstruction)
  116. {
  117. temp.RemoveRange(secondInstruction, temp.Count - secondInstruction);
  118. Console.WriteLine("[" + string.Join(", ", temp) + "]");
  119. }
  120. }
  121. }
  122. else if (instructions[0] == "last")
  123. {
  124. int secondInstruction = int.Parse(instructions[1]);
  125. if (secondInstruction > numbers.Length)
  126. {
  127. Console.WriteLine("Invalid count");
  128. continue;
  129. }
  130. else if (instructions[2] == "odd")
  131. {
  132. List<int> temp = FirstOdds(numbers, secondInstruction);
  133. if (temp.Count <= secondInstruction)
  134. {
  135. Console.WriteLine("[" + string.Join(", ", temp) + "]");
  136. }
  137. else if (temp.Count > secondInstruction)
  138. {
  139. temp.RemoveRange(0, temp.Count - secondInstruction);
  140. Console.WriteLine("[" + string.Join(", ", temp) + "]");
  141. }
  142. }
  143. else if (instructions[2] == "even")
  144. {
  145. List<int> temp = FirstEvens(numbers, secondInstruction);
  146. if (temp.Count <= secondInstruction)
  147. {
  148. Console.WriteLine("[" + string.Join(", ", temp) + "]");
  149. }
  150. else if (temp.Count > secondInstruction)
  151. {
  152. temp.RemoveRange(0, temp.Count - secondInstruction);
  153. Console.WriteLine("[" + string.Join(", ", temp) + "]");
  154. }
  155. }
  156. }
  157. }
  158. Console.WriteLine("[" + string.Join(", ", numbers) + "]");
  159. }
  160. static int[] Exchange(int[] nums, int index)
  161. {
  162. int[] firstHalf = new int[index + 1];
  163. int[] secondHalf = new int[nums.Length];
  164. if (index >= 0 && index < nums.Length - 1)
  165. {
  166. Array.Copy(nums, 0, firstHalf, 0, index + 1);
  167. Array.Copy(nums, index + 1, secondHalf, 0, secondHalf.Length - firstHalf.Length);
  168. Array.Copy(firstHalf, 0, secondHalf, secondHalf.Length - firstHalf.Length, firstHalf.Length);
  169. return secondHalf;
  170. }
  171. else
  172. {
  173. return nums;
  174. }
  175.  
  176. }
  177.  
  178. static int MaxOdd(int[] nums)
  179. {
  180. List<int> odds = new List<int>();
  181. for (int i = 0; i < nums.Length; i++)
  182. {
  183. if (nums[i] % 2 != 0)
  184. {
  185. odds.Add(nums[i]);
  186. }
  187. }
  188. if (odds.Count == 0)
  189. {
  190. return -1;
  191. }
  192. else
  193. {
  194. int max = odds.Max();
  195. return nums.ToList().LastIndexOf(max);
  196. }
  197. }
  198.  
  199. static int MaxEven(int[] nums)
  200. {
  201. List<int> evens = new List<int>();
  202. for (int i = 0; i < nums.Length; i++)
  203. {
  204. if (nums[i] % 2 == 0)
  205. {
  206. evens.Add(nums[i]);
  207. }
  208. }
  209. if (evens.Count == 0)
  210. {
  211. return -1;
  212. }
  213. else
  214. {
  215. int max = evens.Max();
  216. return nums.ToList().LastIndexOf(max);
  217. }
  218. }
  219.  
  220. static int MinOdd(int[] nums)
  221. {
  222. List<int> odds = new List<int>();
  223. for (int i = 0; i < nums.Length; i++)
  224. {
  225. if (nums[i] % 2 != 0)
  226. {
  227. odds.Add(nums[i]);
  228. }
  229. }
  230. if (odds.Count == 0)
  231. {
  232. return -1;
  233. }
  234. else
  235. {
  236. int max = odds.Min();
  237. return nums.ToList().LastIndexOf(max);
  238. }
  239. }
  240.  
  241. static int MinEven(int[] nums)
  242. {
  243. List<int> evens = new List<int>();
  244. for (int i = 0; i < nums.Length; i++)
  245. {
  246. if (nums[i] % 2 == 0)
  247. {
  248. evens.Add(nums[i]);
  249. }
  250. }
  251. if (evens.Count == 0)
  252. {
  253. return -1;
  254. }
  255. else
  256. {
  257. int max = evens.Min();
  258. return nums.ToList().LastIndexOf(max);
  259. }
  260. }
  261. static List<int> FirstEvens(int[] nums, int len)
  262. {
  263. List<int> evens = new List<int>();
  264. for (int i = 0; i < nums.Length; i++)
  265. {
  266. if (nums[i] % 2 == 0)
  267. {
  268. evens.Add(nums[i]);
  269. }
  270. }
  271. return evens;
  272. }
  273.  
  274. static List<int> FirstOdds(int[] nums, int len)
  275. {
  276. List<int> odds = new List<int>();
  277. for (int i = 0; i < nums.Length; i++)
  278. {
  279. if (nums[i] % 2 != 0)
  280. {
  281. odds.Add(nums[i]);
  282. }
  283. }
  284. return odds;
  285. }
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment