diff --git a/Lab4/Program.cs b/Lab4/Program.cs index 59898f2..8ceb9f4 100644 --- a/Lab4/Program.cs +++ b/Lab4/Program.cs @@ -4,6 +4,8 @@ public class Program { public static void Main() { + } } } + diff --git a/Lab4/Purple.cs b/Lab4/Purple.cs index cb4bd6a..0909a8e 100644 --- a/Lab4/Purple.cs +++ b/Lab4/Purple.cs @@ -117,4 +117,4 @@ public void Task10(int[] array, int N) return (bright, normal, dim); } } -} \ No newline at end of file + } diff --git a/Lab4/White.cs b/Lab4/White.cs index d39815d..9b9dc71 100644 --- a/Lab4/White.cs +++ b/Lab4/White.cs @@ -1,4 +1,4 @@ -namespace Lab4 +namespace Lab4 { public class White { @@ -6,112 +6,255 @@ public double Task1(int[] vector) { double length = 0; - // code here + for (int i = 0; i < vector.Length; i++) + { + length += vector[i] * vector[i]; + } - // end - - return length; + return Math.Sqrt(length); } + public int Task2(int[] array, int P, int Q) { int count = 0; - // code here - - // end + for (int i = 0; i < array.Length; i++) + { + if (array[i] > P && array[i] < Q) + { + count++; + } + } return count; } + public void Task3(int[] array) { - - // code here - - // end - + int maxIndex = 0; + + for (int i = 1; i < array.Length; i++) + { + if (array[i] > array[maxIndex]) + { + maxIndex = i; + } + } + + if (maxIndex == array.Length - 1) + { + return; + } + + int minIndex = maxIndex + 1; + for (int i = maxIndex + 1; i < array.Length; i++) + { + if (array[i] < array[minIndex]) + { + minIndex = i; + } + } + + int temp = array[maxIndex]; + array[maxIndex] = array[minIndex]; + array[minIndex] = temp; } + public void Task4(int[] array) { + int max = array[0]; + int index = 0; - // code here - - // end + for (int i = 1; i < array.Length; i++) + { + if (array[i] > max) + { + max = array[i]; + index = i; + } + } + array[index] = index; } + public int Task5(int[] array, int P) { - int index = 0; + int index = -1; - // code here - - // end + for (int i = 0; i < array.Length; i++) + { + if (array[i] == P) + { + index = i; + break; + } + } return index; } + public void Task6(int[] array) { - - // code here - - // end - + int maxIndex = 0; + + for (int i = 1; i < array.Length; i++) + { + if (array[i] > array[maxIndex]) + { + maxIndex = i; + } + } + + for (int i = 0; i + 1 < maxIndex; i += 2) + { + int temp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = temp; + } } + public int[] Task7(int[] array) { - int[] answer = null; - - // code here + int count = 0; - // end + for (int i = 0; i < array.Length; i++) + { + if (array[i] < 0) + { + count++; + } + } + + int[] answer = new int[count]; + int k = 0; + + for (int i = 0; i < array.Length; i++) + { + if (array[i] < 0) + { + answer[k++] = array[i]; + } + } return answer; } + public void Task8(int[] array) { - - // code here - - // end - + for (int i = 0; i < array.Length - 1; i++) + { + for (int j = i + 1; j < array.Length; j++) + { + if (array[i] > array[j]) + { + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + } + } } + public void Task9(int[] array) { - - // code here - - // end - + int left = 0; + int right = array.Length - 1; + + while (left < right) + { + int temp = array[left]; + array[left] = array[right]; + array[right] = temp; + + left++; + right--; + } } + public int[] Task10(int[] A, int[] B) { - int[] C = null; - - // code here - - // end + if (A.Length == 0) + { + return B; + } + + if (B.Length == 0) + { + return A; + } + + int[] C = new int[A.Length + B.Length]; + int k = 0; + + for (int i = 0; i < A.Length; i++) + { + C[k++] = A[i]; + if (i < B.Length) + { + C[k++] = B[i]; + } + } + + for (int i = A.Length; i < B.Length; i++) + { + C[k++] = B[i]; + } return C; } + public double[] Task11(double a, double b, int n) { - double[] array = null; - - // code here - - // end + if (n <= 0) + { + return null; + } + + double[] array = new double[n]; + + if (a <= b) + { + for (int i = 0; i < n; i++) + { + array[i] = a + i; + } + } + else + { + for (int i = 0; i < n; i++) + { + array[i] = a - i; + } + } return array; } public double[] Task12(double[] raw) { - double[] restored = null; - - // code here - - // end + if (raw.Length < 3) + { + return null; + } + + double[] restored = new double[raw.Length]; + + for (int i = 0; i < raw.Length; i++) + { + restored[i] = raw[i]; + } + + for (int i = 1; i < raw.Length - 1; i++) + { + if (restored[i] == -1 && + restored[i - 1] != -1 && + restored[i + 1] != -1) + { + restored[i] = (restored[i - 1] + restored[i + 1]) / 2; + } + } return restored; } } -} \ No newline at end of file +} diff --git a/Lab4test/PurpleTest.cs b/Lab4test/PurpleTest.cs index 830f4f4..091fd23 100644 --- a/Lab4test/PurpleTest.cs +++ b/Lab4test/PurpleTest.cs @@ -382,20 +382,20 @@ public void Test07() new int[] { 5 } }; var answer = new double[][] { - (new double[] { -0.25, -0.125, 0, 0.125, 0.25, 0.375, 0.375, 1, 0.625, 0.375, 0.125, 0.125, 0.125, 0.25, -0.875, 1, 1, -0.125, 0, 0, 0.625, -0.625, -1, 0.25, 0.375 }), - (new double[] { -0.5, -0.75, -1, -0.75, -0.5, -0.25, -0.25, 1, 0.25, -0.25, -0.75, -0.75, -0.75, -0.5, 0.75, 1, 1, -0.75, -1, -1, 0.25, 0.25, 1, -0.5, -0.25 }), - (new double[] { -0.7142857142857143, -1, -1, -1, -0.7142857142857143, -0.4285714285714286, -0.4285714285714286, 1, 0.1428571428571428, -0.4285714285714286, -1, -1, -1, -0.7142857142857143, 0.7142857142857142, 1, 1, -1, -0.1428571428571429, -0.1428571428571429, 0.1428571428571428, 0.1428571428571428, 1, -0.7142857142857143, -0.4285714285714286 }), - (new double[] { 0.7142857142857142, 1, 1, 1, 0.7142857142857142, 0.4285714285714286, 0.4285714285714286, -1, -0.1428571428571429, 0.4285714285714286, 1, 1, 1, 0.7142857142857142, -0.7142857142857143, -1, -1, 1, 0.1428571428571428, 0.1428571428571428, -0.1428571428571429, -0.1428571428571429, -1, 0.7142857142857142, 0.4285714285714286 }), - (new double[] { 0.5, 0.75, 1, 0.75, 0.5, 0.25, 0.25, -1, -0.25, 0.25, 0.75, 0.75, 0.75, 0.5, -0.75, -1, -1, 0.75, 1, 1, -0.25, -0.25, -1, 0.5, 0.25 }), + new double[] { -0.25, -0.125, 0, 0.125, 0.25, 0.375, 0.375, 1, 0.625, 0.375, 0.125, 0.125, 0.125, 0.25, -0.875, 1, 1, -0.125, 0, 0, 0.625, -0.625, -1, 0.25, 0.375 }, + new double[] { -0.5, -0.75, -1, -0.75, -0.5, -0.25, -0.25, 1, 0.25, -0.25, -0.75, -0.75, -0.75, -0.5, 0.75, 1, 1, -0.75, -1, -1, 0.25, 0.25, 1, -0.5, -0.25 }, + new double[] { -0.7142857142857143, -1, -1, -1, -0.7142857142857143, -0.4285714285714286, -0.4285714285714286, 1, 0.1428571428571428, -0.4285714285714286, -1, -1, -1, -0.7142857142857143, 0.7142857142857142, 1, 1, -1, -0.1428571428571429, -0.1428571428571429, 0.1428571428571428, 0.1428571428571428, 1, -0.7142857142857143, -0.4285714285714286 }, + new double[] { 0.7142857142857142, 1, 1, 1, 0.7142857142857142, 0.4285714285714286, 0.4285714285714286, -1, -0.1428571428571429, 0.4285714285714286, 1, 1, 1, 0.7142857142857142, -0.7142857142857143, -1, -1, 1, 0.1428571428571428, 0.1428571428571428, -0.1428571428571429, -0.1428571428571429, -1, 0.7142857142857142, 0.4285714285714286 }, + new double[] { 0.5, 0.75, 1, 0.75, 0.5, 0.25, 0.25, -1, -0.25, 0.25, 0.75, 0.75, 0.75, 0.5, -0.75, -1, -1, 0.75, 1, 1, -0.25, -0.25, -1, 0.5, 0.25 }, null, null, - (new double[] { -1, 1, -1, 1 }), - (new double[] { 1, -1, 1, -1 }), - (new double[] { 1, -0.6666666666666667, -1, 0.6666666666666667 }), - (new double[] { -0.487603305785124, 1, -0.371900826446281, -0.4049586776859504, -0.4380165289256198, -0.4380165289256198, -0.20661157024793386, -0.5867768595041323, -0.42148760330578516, -1 }), - (new double[] { -1, 0, 1, -1, 0, 1, -1, -1, -1, 0, 1, 1, 1 }), - (new double[] { 0.33333333333333326, -0.6666666666666667, -1, -0.33333333333333337, 0.6666666666666667, 1, 0.33333333333333326, 0.33333333333333326, -0.33333333333333337, -0.6666666666666667, -1, 1, 1 }), - (new double[] { 0, 0.5333333333333334, 1, 1, 1, -1, -1, -0.5333333333333333, 0.1333333333333333, -0.19999999999999996 }), + new double[] { -1, 1, -1, 1 }, + new double[] { 1, -1, 1, -1 }, + new double[] { 1, -0.6666666666666667, -1, 0.6666666666666667 }, + new double[] { -0.487603305785124, 1, -0.371900826446281, -0.4049586776859504, -0.4380165289256198, -0.4380165289256198, -0.20661157024793386, -0.5867768595041323, -0.42148760330578516, -1 }, + new double[] { -1, 0, 1, -1, 0, 1, -1, -1, -1, 0, 1, 1, 1 }, + new double[] { 0.33333333333333326, -0.6666666666666667, -1, -0.33333333333333337, 0.6666666666666667, 1, 0.33333333333333326, 0.33333333333333326, -0.33333333333333337, -0.6666666666666667, -1, 1, 1 }, + new double[] { 0, 0.5333333333333334, 1, 1, 1, -1, -1, -0.5333333333333333, 0.1333333333333333, -0.19999999999999996 }, null }; var test = new double[answer.Length][]; @@ -415,7 +415,7 @@ public void Test07() Assert.AreEqual(answer[i].Length, test[i].Length); for (int j = 0; j < answer[i].Length; j++) { - Assert.AreEqual(answer[i][j], test[i][j], $"Test {i} failed (index {j})"); + Assert.AreEqual(answer[i][j], test[i][j], E, $"Test {i} failed (index {j})"); } } } @@ -698,4 +698,5 @@ public void Test12() } } } -} \ No newline at end of file + +}