Again_89

Za Bari

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