Advertisement
whitestarrr

Untitled

Oct 20th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.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 ArrayManipulator
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14. string[] command = Console.ReadLine().Split();
  15. while (!command[0].Equals("end"))
  16. {
  17. if (command[0].Equals("exchange"))
  18. {
  19. int exchangeIndex = int.Parse(command[1]);
  20. if (exchangeIndex < numbers.Length)
  21. {
  22. int count = int.Parse(command[1]);
  23. int[] first = (numbers.Skip(count + 1).Take(numbers.Length - (count + 1))).ToArray();
  24. int[] second = (numbers.Take(count + 1)).ToArray();
  25. var result = first.Union(second).ToArray();
  26. numbers = result;
  27. }
  28. else
  29. {
  30. Console.WriteLine("Invalid index");
  31. }
  32. }
  33. else if (command[0].Equals("max"))
  34. {
  35. if (command[1].Equals("even"))
  36. {
  37. if (numbers.Any(x => x % 2 == 0))
  38. {
  39. var maxEven = numbers.Where(x => x % 2 == 0).Max();
  40. Console.WriteLine(numbers.ToList().LastIndexOf(maxEven));
  41. }
  42. else
  43. {
  44. Console.WriteLine("No matches");
  45. }
  46. }
  47. else
  48. {
  49. if (numbers.Any(x => x % 2 != 0))
  50. {
  51. var maxOdd = numbers.Where(x => x % 2 != 0).Max();
  52. Console.WriteLine(numbers.ToList().LastIndexOf(maxOdd));
  53. }
  54. else
  55. {
  56. Console.WriteLine("No matches");
  57. }
  58. }
  59. }
  60. else if (command[0].Equals("min"))
  61. {
  62. if (command[1].Equals("even"))
  63. {
  64. if (numbers.Any(x => x % 2 == 0))
  65. {
  66. var minEven = numbers.Where(x => x % 2 == 0).Min();
  67. Console.WriteLine(numbers.ToList().LastIndexOf(minEven));
  68. }
  69. else
  70. {
  71. Console.WriteLine("No matches");
  72. }
  73. }
  74. else
  75. {
  76. if (numbers.Any(x => x % 2 != 0))
  77. {
  78. var minOdd = numbers.Where(x => x % 2 != 0).Min();
  79. Console.WriteLine(numbers.ToList().LastIndexOf(minOdd));
  80. }
  81. else
  82. {
  83. Console.WriteLine("No matches");
  84. }
  85. }
  86. }
  87. else if (command[0].Equals("first"))
  88. {
  89. int count = int.Parse(command[1]);
  90. if (count < numbers.Length)
  91. {
  92. if (command[2].Equals("even"))
  93. {
  94. if (numbers.Any(x => x % 2 == 0))
  95. {
  96. var firstEven = numbers.Where(x => x % 2 == 0).Take(count);
  97. Console.WriteLine("[{0}]", string.Join(", ", firstEven));
  98. }
  99. else
  100. {
  101. Console.WriteLine("No matches");
  102. }
  103. }
  104. else
  105. {
  106. if (numbers.Any(x => x % 2 != 0))
  107. {
  108. var firstOdd = numbers.Where(x => x % 2 != 0).Take(count);
  109. Console.WriteLine("[{0}]", string.Join(", ", firstOdd));
  110. }
  111. else
  112. {
  113. Console.WriteLine("No matches");
  114. }
  115. }
  116. }
  117. else
  118. {
  119. Console.WriteLine("Invalid count");
  120. }
  121. }
  122. else if (command[0].Equals("last"))
  123. {
  124. int count = int.Parse(command[1]);
  125. if (count <= numbers.Length)
  126. {
  127. if (command[2].Equals("even"))
  128. {
  129.  
  130. var lastEven = numbers.Where(x => x % 2 == 0).Reverse().Take(count).Reverse();
  131. Console.WriteLine("[{0}]", string.Join(", ", lastEven));
  132. }
  133. else
  134. {
  135. var lastOdd = numbers.Where(x => x % 2 != 0).Reverse().Take(count).Reverse();
  136. Console.WriteLine("[{0}]", string.Join(", ", lastOdd));
  137. }
  138. }
  139. else
  140. {
  141. Console.WriteLine("Invalid count");
  142. }
  143. }
  144. command = Console.ReadLine().Split();
  145. }
  146. Console.WriteLine("[{0}]", string.Join(", ", numbers));
  147. }
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement