Advertisement
Guest User

Untitled

a guest
Nov 1st, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.81 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 ExamPrep4
  8. {
  9. class ArrayManipulator
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<int> myArray = Console.ReadLine().Split().Select(int.Parse).Where(x=> x>=0 && x<=1000).ToList();
  14.  
  15. while (myArray.Count<1 || myArray.Count>50)
  16. {
  17. myArray = Console.ReadLine().Split().Select(int.Parse).Where(x => x >= 0 && x <= 1000).ToList();
  18. }
  19. while (true)
  20. {
  21. string input = Console.ReadLine();
  22.  
  23. if (input == "end")
  24. {
  25. break;
  26. }
  27.  
  28. string[] commands = input.Split();
  29.  
  30. if (commands[0] == "exchange")
  31. {
  32. int index = int.Parse(commands[1]);
  33. if (index > myArray.Count-1 || index<0)
  34. {
  35. Console.WriteLine("Invalid index");
  36.  
  37. }
  38. else
  39. {
  40. myArray = ExchangeNums(index, myArray);
  41.  
  42. }
  43. }
  44. else if (commands[0] == "max")
  45. {
  46. string evenOrOdd = commands[1];
  47.  
  48. int index = MaxEvenOdd(evenOrOdd, myArray);
  49. if (index >= 0 )
  50. {
  51. Console.WriteLine(index);
  52. }
  53. else
  54. {
  55. Console.WriteLine("No matches");
  56. }
  57. }
  58. else if (commands[0] == "min")
  59. {
  60. string evenOrOdd = commands[1];
  61. int index = MinEvenOdd(evenOrOdd, myArray);
  62. if (index >= 0)
  63. {
  64. Console.WriteLine(index);
  65. }
  66. else
  67. {
  68. Console.WriteLine("No matches");
  69. }
  70. }
  71. else if (commands[0] == "first")
  72. {
  73. int count = int.Parse(commands[1]);
  74. string evenOrOdd = commands[2];
  75.  
  76. while (count <= 0)
  77. {
  78. input = Console.ReadLine();
  79. }
  80. if (count > myArray.Count )
  81. {
  82. Console.WriteLine("Invalid count");
  83. }
  84. else
  85. {
  86. var tempList = FirstEvenOdd(count, evenOrOdd, myArray);
  87. if (tempList.Count > 0)
  88. {
  89. Console.WriteLine($"[{string.Join(", ", tempList)}]");
  90. }
  91. else
  92. {
  93. Console.WriteLine("[]");
  94. }
  95.  
  96.  
  97. }
  98.  
  99. }
  100. else if (commands[0] == "last")
  101. {
  102. int count = int.Parse(commands[1]);
  103. string evenOrOdd = commands[2];
  104.  
  105. while (count <= 0)
  106. {
  107. input = Console.ReadLine();
  108. }
  109.  
  110. if (count > myArray.Count)
  111. {
  112. Console.WriteLine("Invalid count");
  113. }
  114. else
  115. {
  116. var tempList = LastEvenOdd(count, evenOrOdd, myArray);
  117. if (tempList.Count >0)
  118. {
  119. Console.WriteLine($"[{string.Join(", ", tempList)}]");
  120. }
  121. else
  122. {
  123. Console.WriteLine("[]");
  124. }
  125.  
  126.  
  127. }
  128. }
  129. }
  130. Console.WriteLine($"[{string.Join(", ", myArray)}]");
  131. }
  132.  
  133. private static List<int> LastEvenOdd(int count, string evenOrOdd, List<int> myArray)
  134. {
  135. if (evenOrOdd == "even")
  136. {
  137. myArray = myArray.Where(x => x % 2 == 0).ToList();
  138.  
  139. var temp = myArray.Skip(myArray.Count - count).Take(count).ToList();
  140. return temp;
  141.  
  142. }
  143. else if (evenOrOdd == "odd")
  144. {
  145. myArray = myArray.Where(x => x % 2 != 0).ToList();
  146.  
  147. var temp = myArray.Skip(myArray.Count - count).Take(count).ToList();
  148. return temp;
  149.  
  150. }
  151. return new List<int>();
  152. }
  153.  
  154. private static List<int> FirstEvenOdd(int count, string evenOrOdd, List<int> myArray)
  155. {
  156. if (evenOrOdd == "even")
  157. {
  158. myArray = myArray.Where(x => x % 2 == 0).ToList();
  159.  
  160. var temp = myArray.Take(count).ToList();
  161. return temp;
  162.  
  163. }
  164. else if (evenOrOdd == "odd")
  165. {
  166. myArray = myArray.Where(x => x % 2 != 0).ToList();
  167.  
  168. var temp = myArray.Take(count).ToList();
  169. return temp;
  170.  
  171. }
  172.  
  173.  
  174.  
  175.  
  176. return new List<int>();
  177. }
  178.  
  179. private static int MinEvenOdd(string evenOrOdd, List<int> myArray)
  180. {
  181. if (evenOrOdd == "even")
  182. {
  183. List<int> evenList = myArray.Where(x => x % 2 == 0).ToList();
  184. if (evenList.Count > 0)
  185. {
  186. int best = evenList.Min();
  187. int index = myArray.IndexOf(best);
  188. if (index >= 0)
  189. {
  190. return index;
  191. }
  192. }
  193. }
  194. else if (evenOrOdd == "odd")
  195. {
  196. List<int> oddList = myArray.Where(x => x % 2 != 0).ToList();
  197. if (oddList.Count > 0)
  198. {
  199. int best = oddList.Min();
  200. int index = myArray.IndexOf(best);
  201. if (index >= 0)
  202. {
  203. return index;
  204. }
  205. }
  206. }
  207. return -1;
  208. }
  209.  
  210. private static int MaxEvenOdd(string evenOrOdd, List<int> myArray)
  211. {
  212. if (evenOrOdd == "even")
  213. {
  214. List<int> evenList = myArray.Where(x => x % 2 == 0).ToList();
  215. if (evenList.Count > 0)
  216. {
  217. int best = evenList.Max();
  218. int index = myArray.IndexOf(best);
  219. if (index >= 0)
  220. {
  221. return index;
  222. }
  223. }
  224. }
  225. else if (evenOrOdd == "odd")
  226. {
  227. List<int> oddList = myArray.Where(x => x % 2 != 0).ToList();
  228. if (oddList.Count > 0 )
  229. {
  230. int best = oddList.Max();
  231. int index = myArray.IndexOf(best);
  232. if (index >= 0)
  233. {
  234. return index;
  235. }
  236. }
  237. }
  238. return -1;
  239. }
  240. private static List<int> ExchangeNums(int index, List<int> myArray)
  241. {
  242. List<int> listAfterExchange = new List<int>();
  243. listAfterExchange.AddRange(myArray.Skip(index + 1));
  244. listAfterExchange.AddRange(myArray.Take(index + 1));
  245.  
  246. return listAfterExchange;
  247.  
  248.  
  249. }
  250. }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement