Advertisement
Guest User

Array Manipulator

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