From 5b9049c847897b80378617030ac6da9005051b5b Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon <74432326+Pigolitsyn@users.noreply.github.com> Date: Tue, 8 Feb 2022 17:58:02 +0300 Subject: [PATCH 01/15] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e58af8a..fc07601 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # Tprogramming_42_2020 -Master branch :) \ No newline at end of file +Pigolitsyn Semyon Michalovich + +Master branch :) From 4ecde77d49f4ad716d9e10bb1f4a1bde5ccce99b Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon <74432326+Pigolitsyn@users.noreply.github.com> Date: Tue, 8 Feb 2022 18:26:39 +0300 Subject: [PATCH 02/15] Update workflow --- .github/workflows/dotnetcore.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml index dffe441..2edfbfc 100644 --- a/.github/workflows/dotnetcore.yml +++ b/.github/workflows/dotnetcore.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - dotnet: [ '2.1', '5.0.x', '6.0.x' ] + dotnet: [ '6.0.x' ] steps: - uses: actions/checkout@v2 From 75cef680c5297c1f9a237b7ba66d21cc42151910 Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon <74432326+Pigolitsyn@users.noreply.github.com> Date: Thu, 10 Feb 2022 15:09:15 +0300 Subject: [PATCH 03/15] Add own BubbleSort's realization --- CourseApp/Module2/BubbleSort.cs | 36 +++++++++++++++------------------ 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..cdac263 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -1,38 +1,34 @@ using System; -using System.Collections.Generic; using System.Text; namespace CourseApp.Module2 { public class BubbleSort { - public static void BubbleSortMethod() + public static void BubbleSortMethod(int n, int[] array) { - 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++) + StringBuilder sb = new StringBuilder(); + bool swaps = false; + for (int i = 0; i < n; ++i) { - arr[i] = int.Parse(sValues[i]); - } - - for (int i = 0; i < arr.Length - 1; i++) - { - for (int j = 0; j < arr.Length - i - 1; j++) + for (int j = 0; j < n - 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).ToString()); + sb.Clear(); + swaps = true; } } } - string result = string.Join(" ", arr); - Console.WriteLine(result); + if (swaps == false) + { + Console.WriteLine(0); + } else { + swaps = false; + } } } } From c5a4be4b98b427bef3637e1ca0076422d7566059 Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon <74432326+Pigolitsyn@users.noreply.github.com> Date: Thu, 10 Feb 2022 15:10:54 +0300 Subject: [PATCH 04/15] Update Program.cs to work with BubbleSort --- CourseApp/Program.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..972d90a 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -7,8 +7,15 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - + + int[] array = new int[Convert.ToInt32(Console.ReadLine())]; + string[] s = Console.ReadLine().Split(' '); + for (int i = 0; i < array.Length; i++) + { + array[i] = Convert.ToInt32(s[i]); + } + + BubbleSort.BubbleSortMethod(array.Length, array); Console.WriteLine("Hello World"); } } From f8fe0431bf85ff05cbc872aefa2343e8c363dffc Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon Date: Thu, 10 Feb 2022 15:24:43 +0300 Subject: [PATCH 05/15] Fix code by stylecop and update BubbleSort.cs to test format --- CourseApp.Tests/CourseApp.Tests.csproj | 45 +++++++++++++------------- CourseApp/CourseApp.csproj | 33 ++++++++++--------- CourseApp/Module2/BubbleSort.cs | 17 +++++++--- CourseApp/Program.cs | 10 +----- _stylecop/stylecop.json | 2 +- 5 files changed, 55 insertions(+), 52 deletions(-) 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/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 cdac263..f45c0e7 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -5,13 +5,20 @@ namespace CourseApp.Module2 { public class BubbleSort { - public static void BubbleSortMethod(int n, int[] array) + public static void BubbleSortMethod() { + int[] array = new int[Convert.ToInt32(Console.ReadLine())]; + string[] s = Console.ReadLine().Split(' '); + for (int i = 0; i < array.Length; i++) + { + array[i] = Convert.ToInt32(s[i]); + } + StringBuilder sb = new StringBuilder(); bool swaps = false; - for (int i = 0; i < n; ++i) + for (int i = 0; i < array.Length; ++i) { - for (int j = 0; j < n - i - 1; ++j) + for (int j = 0; j < array.Length - i - 1; ++j) { if (array[j] > array[j + 1]) { @@ -26,7 +33,9 @@ public static void BubbleSortMethod(int n, int[] array) if (swaps == false) { Console.WriteLine(0); - } else { + } + else + { swaps = false; } } diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index 972d90a..a329120 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -7,15 +7,7 @@ public class Program { public static void Main(string[] args) { - - int[] array = new int[Convert.ToInt32(Console.ReadLine())]; - string[] s = Console.ReadLine().Split(' '); - for (int i = 0; i < array.Length; i++) - { - array[i] = Convert.ToInt32(s[i]); - } - - BubbleSort.BubbleSortMethod(array.Length, array); + 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 From 28229391d6b2b69290e33fa0bf7c5642472bf53d Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon Date: Thu, 10 Feb 2022 15:33:57 +0300 Subject: [PATCH 06/15] Fix test to check last string on output --- CourseApp.Tests/Module2/BubbleSortTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CourseApp.Tests/Module2/BubbleSortTest.cs b/CourseApp.Tests/Module2/BubbleSortTest.cs index 8a9b192..500db34 100644 --- a/CourseApp.Tests/Module2/BubbleSortTest.cs +++ b/CourseApp.Tests/Module2/BubbleSortTest.cs @@ -39,7 +39,7 @@ public void Test1(string input, string expected) // assert var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - Assert.Equal($"{expected}", output[0]); + Assert.Equal($"{expected}", output[^1]); } } } From 2d78f13d7ba22dc2d0996cfd7e1afccf5d9139cd Mon Sep 17 00:00:00 2001 From: "semen.pigolitsyn" Date: Mon, 14 Feb 2022 08:27:11 +0300 Subject: [PATCH 07/15] Add PairSort and tests --- CourseApp.Tests/Module2/PairSortTest.cs | 59 +++++++++++++++++++++++++ CourseApp/Module2/PairSort.cs | 44 ++++++++++++++++++ CourseApp/Program.cs | 1 - 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 CourseApp.Tests/Module2/PairSortTest.cs create mode 100644 CourseApp/Module2/PairSort.cs diff --git a/CourseApp.Tests/Module2/PairSortTest.cs b/CourseApp.Tests/Module2/PairSortTest.cs new file mode 100644 index 0000000..2c1ee69 --- /dev/null +++ b/CourseApp.Tests/Module2/PairSortTest.cs @@ -0,0 +1,59 @@ +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().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } +} 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 a329120..03e4693 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -7,7 +7,6 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); Console.WriteLine("Hello World"); } } From a7ab01d316e114870c0dda6498db6a27f4c5aeb0 Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon Date: Tue, 15 Feb 2022 18:22:04 +0300 Subject: [PATCH 08/15] fix pair sort test --- CourseApp.Tests/Module2/PairSortTest.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/CourseApp.Tests/Module2/PairSortTest.cs b/CourseApp.Tests/Module2/PairSortTest.cs index 2c1ee69..a81c9cf 100644 --- a/CourseApp.Tests/Module2/PairSortTest.cs +++ b/CourseApp.Tests/Module2/PairSortTest.cs @@ -12,20 +12,24 @@ public class PairSortTest : IDisposable private const string Inp1 = @"3 101 80 305 90 -200 14"; +200 14 +"; private const string Out1 = @"305 90 101 80 -200 14"; +200 14 +"; private const string Inp2 = @"3 20 80 30 90 -25 90"; +25 90 +"; private const string Out2 = @"25 90 30 90 -20 80"; +20 80 +"; public void Dispose() { @@ -51,9 +55,7 @@ public void Test1(string input, string expected) PairSort.Sort(); // assert - var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - var result = string.Join(Environment.NewLine, output); - - Assert.Equal($"{expected}", result); + var output = stringWriter.ToString(); + Assert.Equal($"{expected}", output); } } From 767998db13bd0ac55ae94442802e035eef1d5286 Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon Date: Tue, 15 Feb 2022 18:23:16 +0300 Subject: [PATCH 09/15] delete useless using --- CourseApp/Program.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index 03e4693..dd343b3 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,5 +1,4 @@ using System; -using CourseApp.Module2; namespace CourseApp { From 269812b1707e8928b1cb4e1c0e4fd7f7956c89b8 Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon Date: Tue, 15 Feb 2022 18:29:03 +0300 Subject: [PATCH 10/15] Fix BubbleSortTest finally, i hope --- CourseApp.Tests/Module2/BubbleSortTest.cs | 26 ++++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/CourseApp.Tests/Module2/BubbleSortTest.cs b/CourseApp.Tests/Module2/BubbleSortTest.cs index 500db34..3d562af 100644 --- a/CourseApp.Tests/Module2/BubbleSortTest.cs +++ b/CourseApp.Tests/Module2/BubbleSortTest.cs @@ -8,11 +8,21 @@ namespace CourseApp.Tests.Module2 [Collection("Sequential")] public class BubbleSortTest : IDisposable { - private const string Inp1 = @"7 -5 1 7 3 9 4 1"; + private const string Inp1 = @"4 +6 2 3 1"; - private const string Inp2 = @"3 --10 7 2"; + 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() { @@ -24,8 +34,8 @@ public void Dispose() } [Theory] - [InlineData(Inp1, "1 1 3 4 5 7 9")] - [InlineData(Inp2, "-10 2 7")] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] public void Test1(string input, string expected) { var stringWriter = new StringWriter(); @@ -38,8 +48,8 @@ public void Test1(string input, string expected) BubbleSort.BubbleSortMethod(); // assert - var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - Assert.Equal($"{expected}", output[^1]); + var output = stringWriter.ToString(); + Assert.Equal($"{expected}", output); } } } From a0db6c4a8e2e7a9829ed6f766c9df9d92a9879c4 Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon Date: Tue, 22 Feb 2022 10:41:21 +0300 Subject: [PATCH 11/15] merge sort step 1 + add input handler --- CourseApp.Tests/Module2/BubbleSortTest.cs | 69 ++++++++++---------- CourseApp/Module2/BubbleSort.cs | 11 +--- CourseApp/Module2/InputHandler.cs | 18 ++++++ CourseApp/Module2/MergeSort.cs | 79 +++++++++++++++++++++++ 4 files changed, 132 insertions(+), 45 deletions(-) create mode 100644 CourseApp/Module2/InputHandler.cs create mode 100644 CourseApp/Module2/MergeSort.cs diff --git a/CourseApp.Tests/Module2/BubbleSortTest.cs b/CourseApp.Tests/Module2/BubbleSortTest.cs index 3d562af..9064881 100644 --- a/CourseApp.Tests/Module2/BubbleSortTest.cs +++ b/CourseApp.Tests/Module2/BubbleSortTest.cs @@ -3,53 +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 + private const string Inp1 = @"4 6 2 3 1"; - private const string Out1 = @"2 6 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 + private const string Inp2 = @"4 1 2 5 7"; - private const string Out2 = @"0 + private const string Out2 = @"0 "; - 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 - BubbleSort.BubbleSortMethod(); - - // assert - var output = stringWriter.ToString(); - Assert.Equal($"{expected}", output); - } + 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 + BubbleSort.BubbleSortMethod(); + + // assert + var output = stringWriter.ToString(); + Assert.Equal($"{expected}", output); } } diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index f45c0e7..5f4dd71 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -7,12 +7,7 @@ public class BubbleSort { public static void BubbleSortMethod() { - int[] array = new int[Convert.ToInt32(Console.ReadLine())]; - string[] s = Console.ReadLine().Split(' '); - for (int i = 0; i < array.Length; i++) - { - array[i] = Convert.ToInt32(s[i]); - } + int[] array = InputHandler.ArrayHandler(); StringBuilder sb = new StringBuilder(); bool swaps = false; @@ -34,10 +29,6 @@ public static void BubbleSortMethod() { Console.WriteLine(0); } - else - { - swaps = false; - } } } } diff --git a/CourseApp/Module2/InputHandler.cs b/CourseApp/Module2/InputHandler.cs new file mode 100644 index 0000000..38da5f9 --- /dev/null +++ b/CourseApp/Module2/InputHandler.cs @@ -0,0 +1,18 @@ +using System; + +namespace CourseApp.Module2; + +public class InputHandler +{ + public static int[] ArrayHandler() + { + int countElems = Convert.ToInt32(Console.ReadLine()); + int[] array = new int[countElems]; + for (int i = 0; i < countElems; i++) + { + array[i] = Convert.ToInt32(Console.ReadLine()); + } + + 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..648084d --- /dev/null +++ b/CourseApp/Module2/MergeSort.cs @@ -0,0 +1,79 @@ +using System; + +namespace CourseApp.Module2; + +public class MergeSort +{ + public static void Sort() + { + var array = InputHandler.ArrayHandler(); + Sort(array); + } + + 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); + Console.Write(array[0] + " " + array[^1]); + } + + if (rightArray.Length > 1) + { + rightArray = Sort(rightArray); + } + + array = Merge(leftArray, rightArray); + } + + Console.WriteLine(array[0] + " " + array[^1]); + 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 From b66d1c91ff00851ffe27ba32f8c5bd66f77380b4 Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon Date: Sat, 26 Feb 2022 12:50:04 +0300 Subject: [PATCH 12/15] fix bubble sort --- CourseApp/Module2/BubbleSort.cs | 12 ++++++------ CourseApp/Module2/InputHandler.cs | 10 ++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index 5f4dd71..0970049 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -7,18 +7,18 @@ public class BubbleSort { public static void BubbleSortMethod() { - int[] array = InputHandler.ArrayHandler(); + var array = InputHandler.ArrayHandler(); - StringBuilder sb = new StringBuilder(); - bool swaps = false; - for (int i = 0; i < array.Length; ++i) + var sb = new StringBuilder(); + var swaps = false; + for (var i = 0; i < array.Length; ++i) { - for (int j = 0; j < array.Length - i - 1; ++j) + for (var j = 0; j < array.Length - i - 1; ++j) { if (array[j] > array[j + 1]) { (array[j], array[j + 1]) = (array[j + 1], array[j]); - Console.WriteLine("{0}", sb.AppendJoin(" ", array).ToString()); + Console.WriteLine("{0}", sb.AppendJoin(" ", array)); sb.Clear(); swaps = true; } diff --git a/CourseApp/Module2/InputHandler.cs b/CourseApp/Module2/InputHandler.cs index 38da5f9..d882bf5 100644 --- a/CourseApp/Module2/InputHandler.cs +++ b/CourseApp/Module2/InputHandler.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; namespace CourseApp.Module2; @@ -6,11 +7,12 @@ public class InputHandler { public static int[] ArrayHandler() { - int countElems = Convert.ToInt32(Console.ReadLine()); - int[] array = new int[countElems]; - for (int i = 0; i < countElems; i++) + 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(Console.ReadLine()); + array[i] = Convert.ToInt32(inputStrings[i]); } return array; From 8f1d77e11669cc1e5fa518c8ec4614759e01906a Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon Date: Sat, 26 Feb 2022 12:50:34 +0300 Subject: [PATCH 13/15] try to fix mergesort without results --- CourseApp.Tests/Module2/MergeSortTest.cs | 54 ++++++++++++++++++++++++ CourseApp/Module2/MergeSort.cs | 6 +-- 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 CourseApp.Tests/Module2/MergeSortTest.cs 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/Module2/MergeSort.cs b/CourseApp/Module2/MergeSort.cs index 648084d..bd9e779 100644 --- a/CourseApp/Module2/MergeSort.cs +++ b/CourseApp/Module2/MergeSort.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; namespace CourseApp.Module2; @@ -7,7 +8,8 @@ public class MergeSort public static void Sort() { var array = InputHandler.ArrayHandler(); - Sort(array); + var sortedArray = Sort(array); + Console.WriteLine(string.Join(" ", sortedArray)); } private static int[] Sort(int[] array) @@ -20,7 +22,6 @@ private static int[] Sort(int[] array) if (leftArray.Length > 1) { leftArray = Sort(leftArray); - Console.Write(array[0] + " " + array[^1]); } if (rightArray.Length > 1) @@ -31,7 +32,6 @@ private static int[] Sort(int[] array) array = Merge(leftArray, rightArray); } - Console.WriteLine(array[0] + " " + array[^1]); return array; } From a2dce0647d087e5034f564b92f92949062c9fa59 Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon Date: Sat, 26 Feb 2022 13:34:18 +0300 Subject: [PATCH 14/15] add countDiff --- CourseApp.Tests/Module2/CountDiffTest.cs | 46 ++++++++++++++++++++++++ CourseApp/Module2/CountDiff.cs | 27 ++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 CourseApp.Tests/Module2/CountDiffTest.cs create mode 100644 CourseApp/Module2/CountDiff.cs 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/Module2/CountDiff.cs b/CourseApp/Module2/CountDiff.cs new file mode 100644 index 0000000..bfa2031 --- /dev/null +++ b/CourseApp/Module2/CountDiff.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module2; + +public class CountDiff +{ + public static void Count() + { + int s = 0; + var array = InputHandler.ArrayHandler(); + List digits = new List(); + foreach (var sym in array) + { + if (!digits.Contains(sym)) + { + digits.Add(sym); + } + else + { + s++; + } + } + + Console.WriteLine(digits.Count); + } +} \ No newline at end of file From d5d0e16bd862526a6fd41664f5129fc570b7cd3f Mon Sep 17 00:00:00 2001 From: Pigolitsyn Semyon Date: Sat, 26 Feb 2022 14:02:36 +0300 Subject: [PATCH 15/15] delete useless var --- CourseApp/Module2/CountDiff.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CourseApp/Module2/CountDiff.cs b/CourseApp/Module2/CountDiff.cs index bfa2031..4695826 100644 --- a/CourseApp/Module2/CountDiff.cs +++ b/CourseApp/Module2/CountDiff.cs @@ -7,7 +7,6 @@ public class CountDiff { public static void Count() { - int s = 0; var array = InputHandler.ArrayHandler(); List digits = new List(); foreach (var sym in array) @@ -16,10 +15,6 @@ public static void Count() { digits.Add(sym); } - else - { - s++; - } } Console.WriteLine(digits.Count);