Advertisement
Again_89

02. Array Manipulator

Mar 7th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02._Array_Manipulator
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. var collection = Console.ReadLine().Trim().Split().Select(int.Parse).ToList();
  12.  
  13. while (true)
  14. {
  15. var input = Console.ReadLine();
  16. if (input == "end")
  17. {
  18. break;
  19. }
  20.  
  21. var command = input.Split();
  22.  
  23. if (command[0] == "exchange")
  24. {
  25. var index = int.Parse(command[1]);
  26. if (index < 0 || index > collection.Count - 1)
  27. {
  28. Console.WriteLine("Invalid index");
  29. continue;
  30. }
  31. var firstPart = collection.Take(index + 1).ToList();
  32. collection.RemoveRange(0, index + 1);
  33. collection.AddRange(firstPart);
  34. }
  35. else if (command[0] == "max" && command[1] == "even")
  36. {
  37. var collectionHasMaxElement = collection.Where(x => x % 2 == 0).ToList();
  38. if (collectionHasMaxElement.Count == 0)
  39. {
  40. Console.WriteLine("No matches");
  41. }
  42. else
  43. {
  44. var maxEven = collection.Where(x => x % 2 == 0).Max();
  45. var indexOfMaxEven = collection.LastIndexOf(maxEven);
  46. Console.WriteLine(indexOfMaxEven);
  47. }
  48. }
  49. else if (command[0] == "max" && command[1] == "odd")
  50. {
  51. var collectionHasMaxElement = collection.Where(x => x % 2 != 0).ToList();
  52. if (collectionHasMaxElement.Count == 0)
  53. {
  54. Console.WriteLine("No matches");
  55. }
  56. else
  57. {
  58. var maxOdd = collection.Where(x => x % 2 != 0).Max();
  59. var indexOfMaxOdd = collection.LastIndexOf(maxOdd);
  60. Console.WriteLine(indexOfMaxOdd);
  61. }
  62. }
  63. else if (command[0] == "min" && command[1] == "even")
  64. {
  65. var collectionHasMinElement = collection.Where(x => x % 2 == 0).ToList();
  66. if (collectionHasMinElement.Count == 0)
  67. {
  68. Console.WriteLine("No matches");
  69. }
  70. else
  71. {
  72. var minEven = collection.Where(x => x % 2 == 0).Min();
  73. var indexOfMinEven = collection.LastIndexOf(minEven);
  74. Console.WriteLine(indexOfMinEven);
  75. }
  76. }
  77. else if (command[0] == "min" && command[1] == "odd")
  78. {
  79. var collectionHasMinElement = collection.Where(x => x % 2 != 0).ToList();
  80. if (collectionHasMinElement.Count == 0)
  81. {
  82. Console.WriteLine("No matches");
  83. }
  84. else
  85. {
  86. var minOdd = collection.Where(x => x % 2 != 0).Min();
  87. var indexOfMinOdd = collection.LastIndexOf(minOdd);
  88. Console.WriteLine(indexOfMinOdd);
  89. }
  90. }
  91. else if (command[0] == "first" && command[2] == "even")
  92. {
  93. var count = int.Parse(command[1]);
  94. if (count > collection.Count)
  95. {
  96. Console.WriteLine("Invalid count");
  97. }
  98. else
  99. {
  100. var elemtsToPrint = collection.Where(x => x % 2 == 0).Take(count).ToList();
  101. Console.WriteLine($"[{string.Join(", ", elemtsToPrint)}]");
  102. }
  103. }
  104. else if (command[0] == "first" && command[2] == "odd")
  105. {
  106. var count = int.Parse(command[1]);
  107. if (count > collection.Count)
  108. {
  109. Console.WriteLine("Invalid count");
  110. }
  111. else
  112. {
  113. var elemtsToPrint = collection.Where(x => x % 2 != 0).Take(count).ToList();
  114. Console.WriteLine($"[{string.Join(", ", elemtsToPrint)}]");
  115. }
  116. }
  117. else if (command[0] == "last" && command[2] == "even")
  118. {
  119. var count = int.Parse(command[1]);
  120. if (count > collection.Count)
  121. {
  122. Console.WriteLine("Invalid count");
  123. }
  124. else
  125. {
  126. var elemtsToPrint = collection.Where(x => x % 2 == 0).TakeLast(count).ToList();
  127. Console.WriteLine($"[{string.Join(", ", elemtsToPrint)}]");
  128. }
  129. }
  130. else if (command[0] == "last" && command[2] == "odd")
  131. {
  132. var count = int.Parse(command[1]);
  133. if (count > collection.Count)
  134. {
  135. Console.WriteLine("Invalid count");
  136. }
  137. else
  138. {
  139. var elemtsToPrint = collection.Where(x => x % 2 != 0).TakeLast(count).ToList();
  140. Console.WriteLine($"[{string.Join(", ", elemtsToPrint)}]");
  141. }
  142. }
  143. }
  144.  
  145. Console.WriteLine($"[{string.Join(", ", collection)}]");
  146. }
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement