Advertisement
MaxB2

Untitled

Feb 16th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. // Задача: реализовать метод Filter, который должен возвращать входной массив, в котором удалены элементы, которые встречаются более одного раза.
  7. // * Значения в массиве должны следовать в том порядке, в котором они следуют в оригинальном массиве.
  8. // * Если в массиве встречаются повторяющиеся значения, то они ВСЕ значения должны быть удалены.
  9. // * Метод должен выбрасывать исключение ArgumentNullException в случае, если в метод передали null.
  10. // * В решении разрешается использовать только конструкции языка. Использовать LINQ запрещено.
  11.  
  12. public class Program
  13. {
  14. public static int[] Filter(int[] source)
  15. {
  16. if (source != null)
  17. {
  18. var dist = Distinct(source);
  19. return EndofRezults(dist, source);
  20. } throw new ArgumentNullException();
  21. }
  22. public static int[] Distinct(int[] arr)
  23. {
  24. int[] result = new int[] { arr[0] };
  25. int itemsFound = 1;
  26. //проход по исходному массиву
  27. for (int i = 0; i < arr.Length; i++)
  28. {
  29. bool found = false;
  30. int j = 0;
  31.  
  32. // ищем текущий элемент в новом массиве
  33. while (!found && j < itemsFound)
  34. {
  35. if (result[j] == arr[i])
  36. found = true;
  37. j++;
  38. }
  39.  
  40. // если не нашли, добавляем
  41. if (!found)
  42. {
  43. Array.Resize(ref result, itemsFound + 1);
  44. result[itemsFound] = arr[i];
  45. itemsFound++;
  46. }
  47. }
  48.  
  49. // изменяем размер нового массива по количеству найденных элементов
  50. Array.Resize(ref result, itemsFound);
  51. return result;
  52. }
  53. public static int[] EndofRezults(int[] unique, int[] mainArray)
  54. {
  55. List<int> rezult = unique.ToList();
  56. rezult.AddRange(mainArray.ToList());
  57. int[] EndArray = new int[rezult.Count];
  58. bool trigger;
  59. for (int i = 0; i < unique.Length; i++)
  60. {
  61. trigger = true;
  62. for (int j = 0; j < mainArray.Length; j++)
  63. {
  64. if (unique[i] == mainArray[j])
  65. {
  66. trigger = false;
  67. }
  68. }
  69. if (trigger)
  70. {
  71. rezult.Add(unique[i]);
  72. }
  73. }
  74. for (int i = 0; i < unique.Length; i++)
  75. {
  76. trigger = true;
  77. for (int j = 0; j < mainArray.Length; j++)
  78. {
  79. if (mainArray[i] == unique[j])
  80. {
  81. trigger = false;
  82. }
  83. }
  84. if (trigger)
  85. {
  86. rezult.Add(mainArray[i]);
  87. }
  88. }
  89. foreach (int i in rezult)
  90. {
  91. EndArray[i] = rezult[i];
  92. }
  93. return EndArray;
  94. }
  95.  
  96.  
  97. // ДОБАВЬТЕ НОВЫЕ МЕТОДЫ, ЕСЛИ НЕОБХОДИМО
  98.  
  99. // ----- ЗАПРЕЩЕНО ИЗМЕНЯТЬ КОД МЕТОДОВ, КОТОРЫЕ НАХОДЯТСЯ НИЖЕ -----
  100.  
  101. public static void Main()
  102. {
  103. Console.WriteLine("Task is done when all test cases are correct:");
  104.  
  105. int testCaseNumber = 1;
  106.  
  107. TestReturnedValues(testCaseNumber++, new int[] { }, new int[] { });
  108. TestReturnedValues(testCaseNumber++, new int[] { 0 }, new int[] { 0 });
  109. TestReturnedValues(testCaseNumber++, new int[] { 0, 1 }, new int[] { 0, 1 });
  110. TestReturnedValues(testCaseNumber++, new int[] { 0, 0 }, new int[] { });
  111. TestReturnedValues(testCaseNumber++, new int[] { 0, 1, 0 }, new int[] { 1 });
  112. TestReturnedValues(testCaseNumber++, new int[] { 0, 1, 0, 1 }, new int[] { });
  113. TestReturnedValues(testCaseNumber++, new int[] { 0, 1, 2, 2, 5, 4, 4, 5, 1, 8, 4, 9, 1, 3, 4, 5, 7 }, new int[] { 0, 8, 9, 3, 7 });
  114. TestException<ArgumentNullException>(testCaseNumber++, null);
  115.  
  116. if (correctTestCaseAmount == testCaseNumber - 1)
  117. {
  118. Console.WriteLine("Task is done.");
  119. }
  120. else
  121. {
  122. Console.WriteLine("TASK IS NOT DONE.");
  123. }
  124. }
  125.  
  126. private static void TestReturnedValues(int testCaseNumber, int[] array, int[] expectedResult)
  127. {
  128. try
  129. {
  130. var result = Filter(array);
  131.  
  132. if (result.SequenceEqual(expectedResult))
  133. {
  134. Console.WriteLine(correctCaseTemplate, testCaseNumber);
  135. correctTestCaseAmount++;
  136. }
  137. else
  138. {
  139. Console.WriteLine(incorrectCaseTemplate, testCaseNumber);
  140. }
  141. }
  142. catch (Exception)
  143. {
  144. Console.WriteLine(correctCaseTemplate, testCaseNumber);
  145. }
  146. }
  147.  
  148. private static void TestException<T>(int testCaseNumber, int[] array) where T : Exception
  149. {
  150. try
  151. {
  152. Filter(array);
  153. Console.WriteLine(incorrectCaseTemplate, testCaseNumber);
  154. }
  155. catch (T)
  156. {
  157. Console.WriteLine(correctCaseTemplate, testCaseNumber);
  158. correctTestCaseAmount++;
  159. }
  160. catch (Exception)
  161. {
  162. Console.WriteLine(incorrectCaseTemplate, testCaseNumber);
  163. }
  164. }
  165.  
  166. private static string correctCaseTemplate = "Case #{0} is correct.";
  167. private static string incorrectCaseTemplate = "Case #{0} IS NOT CORRECT";
  168. private static int correctTestCaseAmount = 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement