diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index ae46394..98316d0 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -1,30 +1,31 @@  - - netcoreapp2.1 - True - 1573,1591,1701;1702;1705 - false - + + net6.0 + True + 1573,1591,1701;1702;1705 + false + 10 + - - - - - - + + + + + + - - - + + + - - ../_stylecop/stylecop.ruleset - true - + + ../_stylecop/stylecop.ruleset + true + - - - + + + diff --git a/CourseApp.Tests/Module2/BubbleSortTest.cs b/CourseApp.Tests/Module2/BubbleSortTest.cs index 8a9b192..9064881 100644 --- a/CourseApp.Tests/Module2/BubbleSortTest.cs +++ b/CourseApp.Tests/Module2/BubbleSortTest.cs @@ -3,43 +3,52 @@ using Xunit; using CourseApp.Module2; -namespace CourseApp.Tests.Module2 +namespace CourseApp.Tests.Module2; + +[Collection("Sequential")] +public class BubbleSortTest : IDisposable { - [Collection("Sequential")] - public class BubbleSortTest : IDisposable + private const string Inp1 = @"4 +6 2 3 1"; + + private const string Out1 = @"2 6 3 1 +2 3 6 1 +2 3 1 6 +2 1 3 6 +1 2 3 6 +"; + + private const string Inp2 = @"4 +1 2 5 7"; + + private const string Out2 = @"0 +"; + + public void Dispose() { - private const string Inp1 = @"7 -5 1 7 3 9 4 1"; - - private const string Inp2 = @"3 --10 7 2"; - - public void Dispose() - { - var standardOut = new StreamWriter(Console.OpenStandardOutput()); - standardOut.AutoFlush = true; - var standardIn = new StreamReader(Console.OpenStandardInput()); - Console.SetOut(standardOut); - Console.SetIn(standardIn); - } - - [Theory] - [InlineData(Inp1, "1 1 3 4 5 7 9")] - [InlineData(Inp2, "-10 2 7")] - public void Test1(string input, string expected) - { - var stringWriter = new StringWriter(); - Console.SetOut(stringWriter); - - var stringReader = new StringReader(input); - Console.SetIn(stringReader); - - // act - BubbleSort.BubbleSortMethod(); - - // assert - var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - Assert.Equal($"{expected}", output[0]); - } + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + BubbleSort.BubbleSortMethod(); + + // assert + var output = stringWriter.ToString(); + Assert.Equal($"{expected}", output); } } diff --git a/CourseApp.Tests/Module2/CountDiffTest.cs b/CourseApp.Tests/Module2/CountDiffTest.cs new file mode 100644 index 0000000..eab0fbe --- /dev/null +++ b/CourseApp.Tests/Module2/CountDiffTest.cs @@ -0,0 +1,46 @@ +using CourseApp.Module2; +using Module2; + +namespace CourseApp.Tests.Module2; + +using System; +using System.IO; +using Xunit; + +[Collection("Sequential")] +public class CountDiffTest : IDisposable +{ + private const string Inp1 = @"5 +1 0 1 2 0 +"; + + private const string Out1 = @"3 +"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + CountDiff.Count(); + + // assert + var output = stringWriter.ToString(); + Assert.Equal($"{expected}", output); + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/MergeSortTest.cs b/CourseApp.Tests/Module2/MergeSortTest.cs new file mode 100644 index 0000000..4fb0623 --- /dev/null +++ b/CourseApp.Tests/Module2/MergeSortTest.cs @@ -0,0 +1,54 @@ +using CourseApp.Module2; + +namespace CourseApp.Tests.Module2; + +using System; +using System.IO; +using Xunit; + +[Collection("Sequential")] +public class MergeSortTest : IDisposable +{ + private const string Inp1 = @"5 +5 4 3 2 1 +"; + + private const string Out1 = @"1 2 3 4 5 +"; + + private const string Inp2 = @"2 +3 1"; + + private const string Out2 = @"1 2 1 3 +1 3"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + MergeSort.Sort(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/PairSortTest.cs b/CourseApp.Tests/Module2/PairSortTest.cs new file mode 100644 index 0000000..a81c9cf --- /dev/null +++ b/CourseApp.Tests/Module2/PairSortTest.cs @@ -0,0 +1,61 @@ +using Module2; + +namespace CourseApp.Tests.Module2; + +using System; +using System.IO; +using Xunit; + +[Collection("Sequential")] +public class PairSortTest : IDisposable +{ + private const string Inp1 = @"3 +101 80 +305 90 +200 14 +"; + + private const string Out1 = @"305 90 +101 80 +200 14 +"; + + private const string Inp2 = @"3 +20 80 +30 90 +25 90 +"; + + private const string Out2 = @"25 90 +30 90 +20 80 +"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + PairSort.Sort(); + + // assert + var output = stringWriter.ToString(); + Assert.Equal($"{expected}", output); + } +} diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index 9551450..9ead239 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -1,23 +1,24 @@  - - Exe - netcoreapp2.1 - True - 1573,1591,1701;1702;1705; - + + Exe + net6.0 + True + 1573,1591,1701;1702;1705; + 10 + - - - + + + - - ../_stylecop/stylecop.ruleset - true - + + ../_stylecop/stylecop.ruleset + true + - - - + + + diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..0970049 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Text; namespace CourseApp.Module2 @@ -8,31 +7,28 @@ public class BubbleSort { public static void BubbleSortMethod() { - int n = int.Parse(Console.ReadLine()); - string s = Console.ReadLine(); - string[] sValues = s.Split(' '); - int[] arr = new int[n]; - for (int i = 0; i < n; i++) - { - arr[i] = int.Parse(sValues[i]); - } + var array = InputHandler.ArrayHandler(); - for (int i = 0; i < arr.Length - 1; i++) + var sb = new StringBuilder(); + var swaps = false; + for (var i = 0; i < array.Length; ++i) { - for (int j = 0; j < arr.Length - i - 1; j++) + for (var j = 0; j < array.Length - i - 1; ++j) { - if (arr[j] > arr[j + 1]) + if (array[j] > array[j + 1]) { - // int temp = arr[j]; - // arr[j] = arr[j + 1]; - // arr[j+1] = temp; - (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]); + (array[j], array[j + 1]) = (array[j + 1], array[j]); + Console.WriteLine("{0}", sb.AppendJoin(" ", array)); + sb.Clear(); + swaps = true; } } } - string result = string.Join(" ", arr); - Console.WriteLine(result); + if (swaps == false) + { + Console.WriteLine(0); + } } } } diff --git a/CourseApp/Module2/CountDiff.cs b/CourseApp/Module2/CountDiff.cs new file mode 100644 index 0000000..4695826 --- /dev/null +++ b/CourseApp/Module2/CountDiff.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module2; + +public class CountDiff +{ + public static void Count() + { + var array = InputHandler.ArrayHandler(); + List digits = new List(); + foreach (var sym in array) + { + if (!digits.Contains(sym)) + { + digits.Add(sym); + } + } + + Console.WriteLine(digits.Count); + } +} \ No newline at end of file diff --git a/CourseApp/Module2/InputHandler.cs b/CourseApp/Module2/InputHandler.cs new file mode 100644 index 0000000..d882bf5 --- /dev/null +++ b/CourseApp/Module2/InputHandler.cs @@ -0,0 +1,20 @@ +using System; +using System.Linq; + +namespace CourseApp.Module2; + +public class InputHandler +{ + public static int[] ArrayHandler() + { + var countElems = Convert.ToInt32(Console.ReadLine()); + var array = new int[countElems]; + var inputStrings = Console.ReadLine().Split(); + for (var i = 0; i < countElems; i++) + { + array[i] = Convert.ToInt32(inputStrings[i]); + } + + return array; + } +} \ No newline at end of file diff --git a/CourseApp/Module2/MergeSort.cs b/CourseApp/Module2/MergeSort.cs new file mode 100644 index 0000000..bd9e779 --- /dev/null +++ b/CourseApp/Module2/MergeSort.cs @@ -0,0 +1,79 @@ +using System; +using System.Linq; + +namespace CourseApp.Module2; + +public class MergeSort +{ + public static void Sort() + { + var array = InputHandler.ArrayHandler(); + var sortedArray = Sort(array); + Console.WriteLine(string.Join(" ", sortedArray)); + } + + private static int[] Sort(int[] array) + { + if (array.Length > 1) + { + int mid = array.Length / 2; + var leftArray = array[new Range(Index.Start, mid)]; + var rightArray = array[new Range(mid, Index.End)]; + if (leftArray.Length > 1) + { + leftArray = Sort(leftArray); + } + + if (rightArray.Length > 1) + { + rightArray = Sort(rightArray); + } + + array = Merge(leftArray, rightArray); + } + + return array; + } + + private static int[] Merge(int[] left, int[] right) + { + int[] result = new int[right.Length + left.Length]; + + int indexLeft = 0, + indexRight = 0, + indexResult = 0; + + while (indexLeft < left.Length || indexRight < right.Length) + { + if (indexLeft < left.Length && indexRight < right.Length) + { + if (left[indexLeft] <= right[indexRight]) + { + result[indexResult] = left[indexLeft]; + indexLeft++; + indexResult++; + } + else + { + result[indexResult] = right[indexRight]; + indexRight++; + indexResult++; + } + } + else if (indexLeft < left.Length) + { + result[indexResult] = left[indexLeft]; + indexLeft++; + indexResult++; + } + else if (indexRight < right.Length) + { + result[indexResult] = right[indexRight]; + indexRight++; + indexResult++; + } + } + + return result; + } +} \ No newline at end of file diff --git a/CourseApp/Module2/PairSort.cs b/CourseApp/Module2/PairSort.cs new file mode 100644 index 0000000..23cca52 --- /dev/null +++ b/CourseApp/Module2/PairSort.cs @@ -0,0 +1,44 @@ +using System; + +namespace Module2 +{ + public class PairSort + { + public static void Sort() + { + int countPair = Convert.ToInt32(Console.ReadLine()); + int[,] pairs = new int[countPair, 2]; + for (int i = 0; i < countPair; i++) + { + var lol = Console.ReadLine().Split(" "); + pairs[i, 0] = Convert.ToInt32(lol[0]); + pairs[i, 1] = Convert.ToInt32(lol[1]); + } + + for (int i = 0; i < (pairs.Length / 2); ++i) + { + for (int j = 0; j < (pairs.Length / 2) - i - 1; ++j) + { + if (pairs[j, 1] < pairs[j + 1, 1]) + { + (pairs[j, 0], pairs[j + 1, 0]) = (pairs[j + 1, 0], pairs[j, 0]); + (pairs[j, 1], pairs[j + 1, 1]) = (pairs[j + 1, 1], pairs[j, 1]); + } + else if (pairs[j, 1] == pairs[j + 1, 1]) + { + if (pairs[j, 0] > pairs[j + 1, 0]) + { + (pairs[j, 0], pairs[j + 1, 0]) = (pairs[j + 1, 0], pairs[j, 0]); + (pairs[j, 1], pairs[j + 1, 1]) = (pairs[j + 1, 1], pairs[j, 1]); + } + } + } + } + + for (int i = 0; i < pairs.Length / 2; i++) + { + Console.WriteLine($"{pairs[i, 0]} {pairs[i, 1]}"); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..dd343b3 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,5 +1,4 @@ using System; -using CourseApp.Module2; namespace CourseApp { @@ -7,8 +6,6 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - Console.WriteLine("Hello World"); } } diff --git a/_stylecop/stylecop.json b/_stylecop/stylecop.json index 4a96e8f..556fd76 100644 --- a/_stylecop/stylecop.json +++ b/_stylecop/stylecop.json @@ -6,7 +6,7 @@ "documentInterfaces": false, "companyName": "Test Company", "copyrightText": "This source code is Copyright © {companyName} and MAY NOT be copied, reproduced,\npublished, distributed or transmitted to or stored in any manner without prior\nwritten consent from {companyName} (www.yourcompany.com).", - "xmlHeader":false + "xmlHeader": false } } } \ No newline at end of file