Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 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. var inputArrayFirst = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  14.  
  15. var resultArr = new int[inputArrayFirst.Length]; //копие на входящия arr и после пази крайния резултат
  16. Array.Copy(inputArrayFirst, resultArr, inputArrayFirst.Length);
  17. var tempArr = new int[inputArrayFirst.Length];
  18.  
  19. var command = Console.ReadLine().Split(' ').Select(x => x.Trim()).ToArray();
  20. var indexResult = 0;
  21.  
  22.  
  23. while (command[0] != "end")
  24. {
  25. if (command[0] == "exchange")
  26. {
  27. var index = int.Parse(command[1]);
  28.  
  29. if (index > resultArr.Length - 1 || index < 0)
  30. {
  31. Console.WriteLine("Invalid index");
  32. command = Console.ReadLine().Split(' ').ToArray();
  33. continue;
  34. }
  35. Array.Copy(resultArr, index + 1, tempArr, 0, (resultArr.Length - (index + 1)));
  36. Array.Copy(resultArr, 0, tempArr, (resultArr.Length - (index + 1)), index + 1);
  37. Array.Copy(tempArr, resultArr, resultArr.Length);
  38. // Console.WriteLine("[" + string.Join(", ", inputArray) + "]"); // позлвам да си проверя дали е ок резултата
  39. }
  40. if (command[0] == "max" || command[0] == "min")
  41. {
  42.  
  43.  
  44. if (command[1] == "even")
  45. {
  46. bool allOdd = resultArr.All(x => x % 2 != 0);
  47.  
  48. if (allOdd)
  49. {
  50. Console.WriteLine("No matches");
  51. command = Console.ReadLine().Split(' ').ToArray();
  52. continue;
  53. }
  54. }
  55. if (command[1] == "odd")
  56. {
  57. bool allEven = resultArr.All(x => x % 2 == 0);
  58.  
  59. if (allEven)
  60. {
  61. Console.WriteLine("No matches");
  62. command = Console.ReadLine().Split(' ').ToArray();
  63. continue;
  64. }
  65. }
  66. if (command[0] == "max" && command[1] == "even")
  67. {
  68. indexResult = resultArr.Select(x => x).Where(x => x % 2 == 0).OrderByDescending(y => y).First();
  69. }
  70. else if (command[0] == "max" && command[1] == "odd")
  71. {
  72. indexResult = resultArr.Select(x => x).Where(x => x % 2 != 0).OrderByDescending(y => y).First();
  73.  
  74. }
  75. else if (command[0] == "min" && command[1] == "even")
  76. {
  77. indexResult = resultArr.Select(x => x).Where(x => x % 2 == 0).OrderByDescending(y => y).Last();
  78.  
  79. }
  80. else if (command[0] == "min" && command[1] == "odd")
  81. {
  82. indexResult = resultArr.Select(x => x).Where(x => x % 2 != 0).OrderByDescending(y => y).Last();
  83.  
  84. }
  85. int maxIndex = resultArr.ToList().LastIndexOf(indexResult);
  86. Console.WriteLine(maxIndex);
  87. }
  88. if (command[0] == "first" || command[0] == "last")
  89. {
  90. var resultList = new List<int>();
  91. var count = int.Parse(command[1]);
  92. bool allEqual = resultArr.Skip(1).All(s => int.Equals(resultArr[0], s));
  93.  
  94. if (count > resultArr.Length)
  95. {
  96. Console.WriteLine("Invalid count");
  97. command = Console.ReadLine().Split(' ').ToArray();
  98. continue;
  99. }
  100. if (command[0] == "first" && command[2] == "even")
  101. {
  102. resultList = resultArr.Select(x => x).Where(x => x % 2 == 0).Take(count).ToList();
  103. }
  104. else if (command[0] == "first" && command[2] == "odd")
  105. {
  106. resultList = resultArr.Select(x => x).Where(x => x % 2 != 0).Take(count).ToList();
  107. }
  108. else if (command[0] == "last" && command[2] == "even")
  109. {
  110. resultList = resultArr.Select(x => x).Where(x => x % 2 == 0).Reverse().Take(count).Reverse().ToList();
  111. }
  112. else if (command[0] == "last" && command[2] == "odd")
  113. {
  114. resultList = resultArr.Select(x => x).Where(x => x % 2 != 0).Reverse().Take(count).Reverse().ToList();
  115. }
  116. Console.WriteLine("[" + string.Join(", ", resultList) + "]");
  117. }
  118. command = Console.ReadLine().Split(' ').ToArray();
  119. }
  120. Console.WriteLine("[" + string.Join(", ", resultArr) + "]");
  121. }
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement