diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index ae46394..79653f6 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + netcoreapp3.1 True 1573,1591,1701;1702;1705 false diff --git a/CourseApp.Tests/Module2/BubblePairSortTest.cs b/CourseApp.Tests/Module2/BubblePairSortTest.cs new file mode 100644 index 0000000..0241d4d --- /dev/null +++ b/CourseApp.Tests/Module2/BubblePairSortTest.cs @@ -0,0 +1,58 @@ +using System; +using System.IO; +using Xunit; +using CourseApp.Module2; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + public class BubblePairSortTest : IDisposable + { + private const string Inp1 = @"3 +190 76 +299 86 +191 26"; + + private const string Out1 = @"299 86 +190 76 +191 26"; + + private const string Inp2 = @"3 +1 3 +3 4 +2 4"; + + private const string Out2 = @"2 4 +3 4 +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 + BubblePairSort.BubblePairSortMethod(); + + // 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/BubbleSortTest.cs b/CourseApp.Tests/Module2/BubbleSortTest.cs index 8a9b192..719a49d 100644 --- a/CourseApp.Tests/Module2/BubbleSortTest.cs +++ b/CourseApp.Tests/Module2/BubbleSortTest.cs @@ -11,8 +11,22 @@ public class BubbleSortTest : IDisposable private const string Inp1 = @"7 5 1 7 3 9 4 1"; + private const string Out1 = @"1 5 7 3 9 4 1 +1 5 3 7 9 4 1 +1 5 3 7 4 9 1 +1 5 3 7 4 1 9 +1 3 5 7 4 1 9 +1 3 5 4 7 1 9 +1 3 5 4 1 7 9 +1 3 4 5 1 7 9 +1 3 4 1 5 7 9 +1 3 1 4 5 7 9 +1 1 3 4 5 7 9"; + private const string Inp2 = @"3 --10 7 2"; +-10 2 7"; + + private const string Out2 = @"0"; public void Dispose() { @@ -24,8 +38,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(); @@ -39,7 +53,8 @@ public void Test1(string input, string expected) // assert var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - Assert.Equal($"{expected}", output[0]); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); } } } diff --git a/CourseApp.Tests/Module2/MergeSortTest.cs b/CourseApp.Tests/Module2/MergeSortTest.cs new file mode 100644 index 0000000..ab4bac1 --- /dev/null +++ b/CourseApp.Tests/Module2/MergeSortTest.cs @@ -0,0 +1,61 @@ +using System; +using System.IO; +using Xunit; +using CourseApp.Module2; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + public class MergeSortTest : IDisposable + { + private const string Inp1 = @"1 +1"; + + private const string Out1 = @"1"; + + private const string Inp2 = @"2 +3 1"; + + private const string Out2 = @"1 2 1 3 +1 3"; + + private const string Inp3 = @"5 +5 4 3 2 1"; + + private const string Out3 = @"1 2 4 5 +4 5 1 2 +3 5 1 3 +1 5 1 5 +1 2 3 4 5"; + + 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)] + [InlineData(Inp3, Out3)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + MergeSort.MergeSortMethod(); + + // 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.Tests/Module2/NumDiffSortTest.cs b/CourseApp.Tests/Module2/NumDiffSortTest.cs new file mode 100644 index 0000000..2f22276 --- /dev/null +++ b/CourseApp.Tests/Module2/NumDiffSortTest.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module2; +using Xunit; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + + public class NumDiffSortTest : 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 + NumDiff.NumDifferent(); + + // 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/NumInversSortTest.cs b/CourseApp.Tests/Module2/NumInversSortTest.cs new file mode 100644 index 0000000..ed6b164 --- /dev/null +++ b/CourseApp.Tests/Module2/NumInversSortTest.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module2; +using Xunit; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + public class NumInversSortTest : IDisposable + { + private const string Inp1 = @"1 +1"; + + private const string Out1 = @"0"; + + private const string Inp2 = @"5 +5 4 3 2 1"; + + private const string Out2 = @"10"; + + 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 + NumInvers.NumInversion(); + + // 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/RadixSortTest.cs b/CourseApp.Tests/Module2/RadixSortTest.cs new file mode 100644 index 0000000..d300bb0 --- /dev/null +++ b/CourseApp.Tests/Module2/RadixSortTest.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module2; +using Xunit; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + + public class RadixSortTest : IDisposable + { + private const string Inp1 = @"9 +12 +32 +45 +67 +98 +29 +61 +35 +09"; + + private const string Out1 = @"Initial array: +12, 32, 45, 67, 98, 29, 61, 35, 09 +********** +Phase 1 +Bucket 0: empty +Bucket 1: 61 +Bucket 2: 12, 32 +Bucket 3: empty +Bucket 4: empty +Bucket 5: 45, 35 +Bucket 6: empty +Bucket 7: 67 +Bucket 8: 98 +Bucket 9: 29, 09 +********** +Phase 2 +Bucket 0: 09 +Bucket 1: 12 +Bucket 2: 29 +Bucket 3: 32, 35 +Bucket 4: 45 +Bucket 5: empty +Bucket 6: 61, 67 +Bucket 7: empty +Bucket 8: empty +Bucket 9: 98 +********** +Sorted array: +09, 12, 29, 32, 35, 45, 61, 67, 98"; + + 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 + RadixSort.RadixSortMethod(); + + // 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/StorageSortTest.cs b/CourseApp.Tests/Module2/StorageSortTest.cs new file mode 100644 index 0000000..29b20ee --- /dev/null +++ b/CourseApp.Tests/Module2/StorageSortTest.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module2; +using Xunit; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + public class StorageSortTest : IDisposable + { + private const string Inp1 = @"5 +1 50 3 4 3 +16 +1 2 3 4 5 1 3 3 4 5 5 5 5 5 4 5"; + + private const string Out1 = @"yes +no +no +no +yes"; + + 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 + StorageSort.CountOrders(); + + // 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.Tests/Module3/CycShiftTest.cs b/CourseApp.Tests/Module3/CycShiftTest.cs new file mode 100644 index 0000000..44d2de7 --- /dev/null +++ b/CourseApp.Tests/Module3/CycShiftTest.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module3; +using Xunit; + +namespace CourseApp.Tests.Module3 +{ + [Collection("Sequential")] + public class CycShiftTest : IDisposable + { + private const string Inp1 = @"a +b"; + + private const string Inp2 = @"zabcd +abcdz"; + + private const string Out1 = @"-1"; + + private const string Out2 = @"4"; + + 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 + CycShift.Start(); + + // 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.Tests/Module3/CycStringTest.cs b/CourseApp.Tests/Module3/CycStringTest.cs new file mode 100644 index 0000000..55a4829 --- /dev/null +++ b/CourseApp.Tests/Module3/CycStringTest.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module3; +using Xunit; + +namespace CourseApp.Tests.Module3 +{ + [Collection("Sequential")] + public class CycStringTest : IDisposable + { + private const string Inp1 = @"z +"; + + private const string Out1 = @"1"; + + 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 + CycString.Start(); + + // 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.Tests/Module3/PerRowTest.cs b/CourseApp.Tests/Module3/PerRowTest.cs new file mode 100644 index 0000000..1f210a1 --- /dev/null +++ b/CourseApp.Tests/Module3/PerRowTest.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module3; +using Xunit; + +namespace CourseApp.Tests.Module3 +{ + [Collection("Sequential")] + public class PerRowTest : IDisposable + { + private const string Inp1 = @"aaaaa +"; + + private const string Inp2 = @"abcabcabc +"; + + private const string Inp3 = @"abab +"; + + private const string Out1 = @"5"; + + private const string Out2 = @"3"; + + private const string Out3 = @"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, Out1)] + [InlineData(Inp2, Out2)] + [InlineData(Inp3, Out3)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + PerRow.Start(); + + // 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.Tests/Module3/SubStringTest.cs b/CourseApp.Tests/Module3/SubStringTest.cs new file mode 100644 index 0000000..fe78c4c --- /dev/null +++ b/CourseApp.Tests/Module3/SubStringTest.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module3; +using Xunit; + +namespace CourseApp.Tests.Module3 +{ + [Collection("Sequential")] + public class SubStringTest : IDisposable + { + private const string Inp1 = @"ababbababa +aba"; + + private const string Out1 = @"0 5 7 "; + + 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 + SubString.Start(); + + // 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.Tests/Module4/CorrectBracketsTest.cs b/CourseApp.Tests/Module4/CorrectBracketsTest.cs new file mode 100644 index 0000000..c43da78 --- /dev/null +++ b/CourseApp.Tests/Module4/CorrectBracketsTest.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module4; +using Xunit; + +namespace CourseApp.Tests.Module4 +{ + [Collection("Sequential")] + public class CorrectBracketsTest : IDisposable + { + private const string Inp1 = @"())(()"; + private const string Inp2 = @"))((("; + + private const string Out1 = @"2"; + private const string Out2 = @"5"; + + 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 + CorrectBrackets.Brackets(); + + // 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.Tests/Module4/DeadEndSortTest.cs b/CourseApp.Tests/Module4/DeadEndSortTest.cs new file mode 100644 index 0000000..341031d --- /dev/null +++ b/CourseApp.Tests/Module4/DeadEndSortTest.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module4; +using Xunit; + +namespace CourseApp.Tests.Module4 + { + [Collection("Sequential")] + public class DeadEndSortTest : IDisposable + { + private const string Inp1 = @"3 +3 2 1"; + + private const string Inp2 = @"4 +4 1 3 2"; + + private const string Inp3 = @"3 +2 3 1"; + + private const string Out1 = @"YES"; + private const string Out2 = @"YES"; + private const string Out3 = @"NO"; + + 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)] + [InlineData(Inp3, Out3)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + DeadEndSort.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.Tests/Module4/MinSegmentsTest.cs b/CourseApp.Tests/Module4/MinSegmentsTest.cs new file mode 100644 index 0000000..13f056d --- /dev/null +++ b/CourseApp.Tests/Module4/MinSegmentsTest.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module4; +using Xunit; + +namespace CourseApp.Tests.Module4 +{ + [Collection("Sequential")] + public class MinSegmentsTest : IDisposable + { + private const string Inp1 = @"7 3 +1 3 2 4 5 3 1"; + + private const string Out1 = @"1 +2 +2 +3 +1"; + + 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 + MinSegments.MinSegmentsMethod(); + + // 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.Tests/Module4/NearestMinTest.cs b/CourseApp.Tests/Module4/NearestMinTest.cs new file mode 100644 index 0000000..4d0e8bf --- /dev/null +++ b/CourseApp.Tests/Module4/NearestMinTest.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module4; +using Xunit; + +namespace CourseApp.Tests.Module4 +{ + [Collection("Sequential")] + public class NearestMinTest : IDisposable + { + private const string Inp1 = @"10 +1 2 3 2 1 4 2 5 3 1"; + + private const string Out1 = @"-1 4 3 4 -1 6 9 8 9 -1"; + + 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 + NearestMin.NearestMinMethod(); + + // 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.Tests/Module5/BinaryTreeBranchesTest.cs b/CourseApp.Tests/Module5/BinaryTreeBranchesTest.cs new file mode 100644 index 0000000..bc05687 --- /dev/null +++ b/CourseApp.Tests/Module5/BinaryTreeBranchesTest.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module5; +using Xunit; + +namespace CourseApp.Tests.Module5 +{ + [Collection("Sequential")] + public class BinaryTreeBranchesTest : IDisposable + { + private const string Inp1 = @"7 3 2 1 9 5 4 6 8 0"; + + private const string Out1 = @"2 +9"; + + 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 + BinaryTreeBranches.Binary_Tree.BinaryTreeBranchesMethod(); + + // 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.Tests/Module5/TreeBalanceCheckTest.cs b/CourseApp.Tests/Module5/TreeBalanceCheckTest.cs new file mode 100644 index 0000000..b91f52a --- /dev/null +++ b/CourseApp.Tests/Module5/TreeBalanceCheckTest.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.IO; +using CourseApp.Module5; +using Xunit; + +namespace CourseApp.Tests.Module5 +{ + [Collection("Sequential")] + public class TreeBalanceCheckTest : IDisposable + { + private const string Inp1 = @"7 3 2 1 9 5 4 6 8 0"; + + private const string Out1 = @"YES"; + + 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 + TreeBalanceCheck.Binary_Tree.TreeBalanceCheckMethod(); + + // 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/CourseApp.csproj b/CourseApp/CourseApp.csproj index 9551450..44eee5b 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp2.1 + netcoreapp3.1 True 1573,1591,1701;1702;1705; diff --git a/CourseApp/Module2/BubblePairSort.cs b/CourseApp/Module2/BubblePairSort.cs new file mode 100644 index 0000000..4228c4c --- /dev/null +++ b/CourseApp/Module2/BubblePairSort.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class BubblePairSort + { + public static void BubblePairSortMethod() + { + int cols = Convert.ToInt32(Console.ReadLine()); + int rows = 2; + int[,] mas = new int[cols, rows]; + + string input; + string[] input_mas; + + for (int i = 0; i < cols; i++) + { + input = Console.ReadLine(); + input_mas = input.Split(' '); + + mas[i, 0] = Convert.ToInt32(input_mas[0]); + mas[i, 1] = Convert.ToInt32(input_mas[1]); + } + + int tmp_id = 0; + int tmp_value = 0; + + for (int j = 0; j < cols - 1; j++) + { + for (int i = 0; i < cols - 1; i++) + { + if (mas[i, 1] < mas[i + 1, 1]) + { + tmp_id = mas[i, 0]; + tmp_value = mas[i, 1]; + mas[i, 0] = mas[i + 1, 0]; + mas[i, 1] = mas[i + 1, 1]; + mas[i + 1, 0] = tmp_id; + mas[i + 1, 1] = tmp_value; + } + else if (mas[i, 1] == mas[i + 1, 1]) + { + if (mas[i, 0] > mas[i + 1, 0]) + { + tmp_id = mas[i, 0]; + tmp_value = mas[i, 1]; + mas[i, 0] = mas[i + 1, 0]; + mas[i, 1] = mas[i + 1, 1]; + mas[i + 1, 0] = tmp_id; + mas[i + 1, 1] = tmp_value; + } + } + } + } + + for (int i = 0; i <= cols - 1; i++) + { + Console.WriteLine($"{mas[i, 0]} {mas[i, 1]}"); + } + } + } +} diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..1e136e2 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -8,31 +8,36 @@ 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++) + int zero_test = 0; + int c_elems = Convert.ToInt32(Console.ReadLine()); + int[] mas_elems = new int[c_elems]; + string elem_mas = Console.ReadLine(); + string[] elems_mas = elem_mas.Split(' '); + Console.WriteLine(); + + for (int i = 0; i < mas_elems.Length; i++) { - arr[i] = int.Parse(sValues[i]); + mas_elems[i] = int.Parse(elems_mas[i]); } - for (int i = 0; i < arr.Length - 1; i++) + for (int i = 1; i < mas_elems.Length; i++) { - for (int j = 0; j < arr.Length - i - 1; j++) + for (int j = 0; j < mas_elems.Length - i; j++) { - if (arr[j] > arr[j + 1]) + if (mas_elems[j] > mas_elems[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]); + (mas_elems[j], mas_elems[j + 1]) = (mas_elems[j + 1], mas_elems[j]); + zero_test++; + string result = string.Join(" ", mas_elems); + Console.WriteLine(result); } } } - string result = string.Join(" ", arr); - Console.WriteLine(result); + if (zero_test == 0) + { + Console.WriteLine(zero_test); + } } } } diff --git a/CourseApp/Module2/MergeSort.cs b/CourseApp/Module2/MergeSort.cs new file mode 100644 index 0000000..bb7ad35 --- /dev/null +++ b/CourseApp/Module2/MergeSort.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class MergeSort + { + public static void MergeSortMethod() + { + int n = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + string[] s_mas = s.Split(" "); + int[] mas = new int[n]; + for (int i = 0; i < n; i++) + { + mas[i] = int.Parse(s_mas[i]); + } + + MergeSortS(mas, 0, n); + DisplayArray(mas); + } + + private static void MergeSortS(int[] array, int left_bord, int right_bord) + { + if (array.Length == 1) + { + return; + } + + int middle = array.Length / 2; + int[] left = new int[middle]; + int[] right = new int[array.Length - middle]; + + for (int i = 0; i < middle; i++) + { + left[i] = array[i]; + } + + for (int i = middle; i < array.Length; i++) + { + right[i - middle] = array[i]; + } + + int middle_s = (left_bord + right_bord) / 2; + + MergeSortS(left, left_bord, middle_s); + MergeSortS(right, middle_s, right_bord); + + Console.Write($"{left_bord + 1} {right_bord}"); + + Merge(array, left, right); + } + + private static void Merge(int[] targetArray, int[] mas1, int[] mas2) + { + int mas1MinIndex = 0; + int mas2MinIndex = 0; + int targerArrayMinIndex = 0; + + while (mas1MinIndex < mas1.Length && mas2MinIndex < mas2.Length) + { + if (mas1[mas1MinIndex] <= mas2[mas2MinIndex]) + { + targetArray[targerArrayMinIndex] = mas1[mas1MinIndex]; + mas1MinIndex++; + } + else + { + targetArray[targerArrayMinIndex] = mas2[mas2MinIndex]; + mas2MinIndex++; + } + + targerArrayMinIndex++; + } + + while (mas1MinIndex < mas1.Length) + { + targetArray[targerArrayMinIndex] = mas1[mas1MinIndex]; + mas1MinIndex++; + targerArrayMinIndex++; + } + + while (mas2MinIndex < mas2.Length) + { + targetArray[targerArrayMinIndex] = mas2[mas2MinIndex]; + mas2MinIndex++; + targerArrayMinIndex++; + } + + Console.WriteLine($" {targetArray[0]} {targetArray[targetArray.Length - 1]}"); + } + + private static void DisplayArray(int[] array) + { + Console.WriteLine("{0}", string.Join(" ", array)); + } + } +} diff --git a/CourseApp/Module2/NumDiff.cs b/CourseApp/Module2/NumDiff.cs new file mode 100644 index 0000000..d19074c --- /dev/null +++ b/CourseApp/Module2/NumDiff.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class NumDiff + { + private static int count = 1; + + public static void NumDifferent() + { + 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]); + } + + QuickSort(arr, 0, n - 1); + for (int i = 1; i < n; i++) + { + if (arr[i - 1] != arr[i]) + { + count += 1; + } + } + + Console.WriteLine("{0}", count); + } + + public static int Partition(int[] arr, int l, int r) + { + int p = arr[l]; + int i = l - 1, j = r + 1; + + while (true) + { + do + { + i++; + } + while (arr[i] < p); + do + { + j--; + } + while (arr[j] > p); + if (i >= j) + { + return j; + } + + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + public static void QuickSort(int[] arr, int l, int r) + { + if (l < r) + { + int q = Partition(arr, l, r); + + QuickSort(arr, l, q); + QuickSort(arr, q + 1, r); + } + } + } +} diff --git a/CourseApp/Module2/NumInvers.cs b/CourseApp/Module2/NumInvers.cs new file mode 100644 index 0000000..859ad1b --- /dev/null +++ b/CourseApp/Module2/NumInvers.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class NumInvers + { + private static long count = 0; + + public static void NumInversion() + { + int n = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + string[] s_mas = s.Split(" "); + int[] mas = new int[n]; + for (int i = 0; i < n; i++) + { + mas[i] = int.Parse(s_mas[i]); + } + + MergeSort(mas, 0, n); + Console.WriteLine($"{count}"); + } + + public static void MergeSort(int[] array, int left_bord, int right_bord) + { + if (array.Length == 1) + { + return; + } + + int middle = array.Length / 2; + int[] left = new int[middle]; + int[] right = new int[array.Length - middle]; + + for (int i = 0; i < middle; i++) + { + left[i] = array[i]; + } + + for (int i = middle; i < array.Length; i++) + { + right[i - middle] = array[i]; + } + + int middle_s = (left_bord + right_bord) / 2; + + MergeSort(left, left_bord, middle_s); + MergeSort(right, middle_s, right_bord); + Merge(array, left, right); + } + + public static void Merge(int[] targetArray, int[] mas1, int[] mas2) + { + int mas1MinIndex = 0; + int mas2MinIndex = 0; + int targerArrayMinIndex = 0; + + while (mas1MinIndex < mas1.Length && mas2MinIndex < mas2.Length) + { + if (mas1[mas1MinIndex] <= mas2[mas2MinIndex]) + { + targetArray[targerArrayMinIndex] = mas1[mas1MinIndex]; + mas1MinIndex++; + } + else + { + targetArray[targerArrayMinIndex] = mas2[mas2MinIndex]; + mas2MinIndex++; + count += mas1.Length - mas1MinIndex; + } + + targerArrayMinIndex++; + } + + while (mas1MinIndex < mas1.Length) + { + targetArray[targerArrayMinIndex] = mas1[mas1MinIndex]; + mas1MinIndex++; + targerArrayMinIndex++; + } + + while (mas2MinIndex < mas2.Length) + { + targetArray[targerArrayMinIndex] = mas2[mas2MinIndex]; + mas2MinIndex++; + targerArrayMinIndex++; + } + } + } +} diff --git a/CourseApp/Module2/RadixSort.cs b/CourseApp/Module2/RadixSort.cs new file mode 100644 index 0000000..daaa31c --- /dev/null +++ b/CourseApp/Module2/RadixSort.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +public static class RadixSort +{ + public static void RadixSortMethod() + { + int size = int.Parse(Console.ReadLine()); + List array = new List(); + for (int i = 0; i < size; i++) + { + array.Add(Console.ReadLine()); + } + + Console.WriteLine("Initial array:"); + for (int i = 0; i < size; i++) + { + if (i < size - 1) + { + Console.Write(array[i] + ", "); + } + else + { + Console.WriteLine(array[i]); + } + } + + int grade = array[0].Length; + array = Sort(array, grade); + Console.WriteLine("**********"); + Console.WriteLine("Sorted array:"); + for (int i = 0; i < size; i++) + { + if (i < size - 1) + { + Console.Write(array[i] + ", "); + } + else + { + Console.WriteLine(array[i]); + } + } + } + + public static List Sort(List array, int grade) + { + int phase = 1; + int length = array[0].Length; + + for (int i = 0; i < grade; i++) + { + Console.WriteLine("**********"); + Console.WriteLine("Phase {0}", phase); + + List[] bin = new List[10]; + for (int t = 0; t < 10; t++) + { + bin[t] = new List(); + } + + for (int j = 0; j < array.Count; j++) + { + int k = int.Parse(array[j].Substring(length - phase, 1)); + bin[k].Add(array[j]); + } + + for (int j = 0; j < 10; j++) + { + if (bin[j].Count == 0) + { + Console.WriteLine("Bucket {0}: empty", j); + } + else + { + string outString = null; + + for (int c = 0; c < bin[j].Count; c++) + { + if (c < bin[j].Count - 1) + { + outString += bin[j][c] + ", "; + } + else + { + outString += bin[j][c]; + } + } + + Console.WriteLine("Bucket {0}: {1}", j, outString); + } + } + + int p = 0; + for (int j = 0; j < 10; j++) + { + for (int k = 0; k < bin[j].Count; k++) + { + array[p] = bin[j][k]; + p += 1; + } + } + + phase += 1; + } + + return array; + } +} \ No newline at end of file diff --git a/CourseApp/Module2/StorageSort.cs b/CourseApp/Module2/StorageSort.cs new file mode 100644 index 0000000..c54df9c --- /dev/null +++ b/CourseApp/Module2/StorageSort.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class StorageSort + { + public static void CountOrders() + { + int count_position = int.Parse(Console.ReadLine()); + string tmp_arr = Console.ReadLine(); + string[] sValues = tmp_arr.Split(' '); + int[] storage = new int[count_position]; + for (int i = 0; i < count_position; i++) + { + storage[i] = int.Parse(sValues[i]); + } + + int count_orders = int.Parse(Console.ReadLine()); + tmp_arr = Console.ReadLine(); + sValues = tmp_arr.Split(' '); + int[] order = new int[count_orders]; + for (int i = 0; i < count_orders; i++) + { + order[i] = int.Parse(sValues[i]); + } + + int[] counting = new int[count_position]; + CheckOrder(order, counting, count_orders); + + for (int i = 0; i < count_position; i++) + { + if (counting[i] <= storage[i]) + { + Console.WriteLine("no"); + } + else + { + Console.WriteLine("yes"); + } + } + } + + public static void CheckOrder(int[] order, int[] count, int count_order) + { + for (int i = 0; i < count_order; i++) + { + count[order[i] - 1]++; + } + } + } +} diff --git a/CourseApp/Module3/CycShift.cs b/CourseApp/Module3/CycShift.cs new file mode 100644 index 0000000..9aad4b3 --- /dev/null +++ b/CourseApp/Module3/CycShift.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; + +namespace CourseApp.Module3 +{ + public class CycShift + { + public static int Methot_hash(string s, string t) + { + if (s == t) + { + return 0; + } + + t = string.Concat(Enumerable.Repeat(t, 2)); + + long p = 13; + long m = 1; + long q = (long)(Math.Pow(2, 31) - 1); + + long first_hash = 0; + long second_hash = 0; + long xt = 1; + + foreach (char i in s.Reverse()) + { + first_hash = (first_hash + (i * m)) % q; + m = (m * p) % q; + } + + m = 1; + + for (int i = s.Length - 1; i >= 0; i--) + { + second_hash = (second_hash + (t[i] * m)) % q; + m = (m * p) % q; + } + + for (int i = 0; i < s.Length - 1; i++) + { + xt = (xt * p) % q; + } + + for (int i = 1; i < t.Length - s.Length + 1; i++) + { + if (first_hash == second_hash) + { + return i - 1; + } + + second_hash = p * (second_hash - (t[i - 1] * xt)); + second_hash += t[i + s.Length - 1]; + second_hash = second_hash % q; + + if ((second_hash < 0 && q > 0) || (second_hash > 0 && q < 0)) + { + second_hash += q; + } + } + + return -1; + } + + public static void Start() + { + string s = Console.ReadLine(); + string t = Console.ReadLine(); + + int result = Methot_hash(s, t); + + Console.WriteLine(result); + } + } +} diff --git a/CourseApp/Module3/CycString.cs b/CourseApp/Module3/CycString.cs new file mode 100644 index 0000000..0e48c82 --- /dev/null +++ b/CourseApp/Module3/CycString.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module3 +{ + public class CycString + { + public static int[] Prefix_method(string s) + { + int[] res = new int[s.Length]; + res[0] = 0; + + for (int i = 0; i < s.Length - 1; i++) + { + int j = res[i]; + + while (j > 0 && s[i + 1] != s[j]) + { + j = res[j - 1]; + } + + if (s[i + 1] == s[j]) + { + res[i + 1] = j + 1; + } + else + { + res[i + 1] = 0; + } + } + + return res; + } + + public static void Start() + { + string s = Console.ReadLine(); + + int[] prefix = Prefix_method(s); + + int result = s.Length - prefix[s.Length - 1]; + + Console.WriteLine(result); + } + } +} diff --git a/CourseApp/Module3/PerRow.cs b/CourseApp/Module3/PerRow.cs new file mode 100644 index 0000000..b6d9830 --- /dev/null +++ b/CourseApp/Module3/PerRow.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module3 +{ + public class PerRow + { + public static int[] Method_Prefix(string s) + { + int[] res = new int[s.Length]; + res[0] = 0; + + for (int i = 0; i < s.Length - 1; i++) + { + int j = res[i]; + + while (j > 0 && s[i + 1] != s[j]) + { + j = res[j - 1]; + } + + if (s[i + 1] == s[j]) + { + res[i + 1] = j + 1; + } + else + { + res[i + 1] = 0; + } + } + + return res; + } + + public static void Start() + { + string s = Console.ReadLine(); + int[] prefix = Method_Prefix(s); + + int result = s.Length - prefix[s.Length - 1]; + + if (s.Length % result == 0) + { + Console.WriteLine(s.Length / result); + } + else + { + Console.WriteLine(1); + } + } + } +} diff --git a/CourseApp/Module3/SubString.cs b/CourseApp/Module3/SubString.cs new file mode 100644 index 0000000..7a9df3a --- /dev/null +++ b/CourseApp/Module3/SubString.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module3 +{ + public class SubString + { + public static long Get_hash(string s, int n, int p, int x) + { + long res = 0; + for (int i = 0; i < n; i++) + { + res = ((res * x) + s[i]) % p; + } + + return res; + } + + public static void Rabin_Karp(string s, string t, int p, int x) + { + long ht = Get_hash(t, t.Length, p, x); + + long hs = Get_hash(s, t.Length, p, x); + + long xt = 1; + + for (int i = 0; i < t.Length; i++) + { + xt = (xt * x) % p; + } + + for (int i = 0; i <= s.Length - t.Length; i++) + { + if (ht == hs) + { + Console.Write("{0} ", i); + } + + if (i + t.Length < s.Length) + { + hs = ((hs * x) - (s[i] * xt) + s[i + t.Length]) % p; + hs = (hs + p) % p; + } + } + } + + public static void Start() + { + string s = Console.ReadLine(); + string t = Console.ReadLine(); + + int p = (int)(Math.Pow(2, 31) - 1); + int x = 26; + + Rabin_Karp(s, t, p, x); + } + } +} diff --git a/CourseApp/Module4/CorrectBrackets.cs b/CourseApp/Module4/CorrectBrackets.cs new file mode 100644 index 0000000..8a2002e --- /dev/null +++ b/CourseApp/Module4/CorrectBrackets.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Text; +using CourseApp.Module4; + +namespace CourseApp.Module4 +{ + public class CorrectBrackets + { + public static void Brackets() + { + int counter = 0; + string str = Console.ReadLine(); + Stack brackets = new Stack(str.Length); + foreach (char item in str) + { + if (item == '(') + { + brackets.Push(item); + } + else if (brackets.Empty() == false && item == ')') + { + brackets.Pop(); + } + else + { + counter++; + } + } + + Console.WriteLine(counter + brackets.Count); + } + } +} diff --git a/CourseApp/Module4/DeadEndSort.cs b/CourseApp/Module4/DeadEndSort.cs new file mode 100644 index 0000000..a4a1e47 --- /dev/null +++ b/CourseApp/Module4/DeadEndSort.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module4 +{ + public class DeadEndSort + { + public static void Sort() + { + bool trigger = true; + int size = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + string[] trainS = s.Split(' '); + int[] train = new int[size]; + Stack stackTrain = new Stack(size); + for (int i = 0; i < size; i++) + { + train[i] = int.Parse(trainS[i]); + } + + Stack sortVien = new Stack(size); + int[] routeB = new int[size]; + int buff = 0; + int elem = 1; + + for (int i = 0; i < size; i++) + { + sortVien.Push(train[i]); + if (train[i] == elem) + { + routeB[buff] = sortVien.Peek(); + sortVien.Pop(); + buff++; + elem++; + while (sortVien.Empty() ? false : sortVien.Peek() == elem) + { + routeB[buff] = sortVien.Peek(); + sortVien.Pop(); + buff++; + elem++; + } + } + } + + for (int i = 0; i < size - 1; i++) + { + if (routeB[i] > routeB[i + 1]) + { + trigger = false; + break; + } + } + + Console.WriteLine(trigger ? "YES" : "NO"); + } + } +} diff --git a/CourseApp/Module4/MinSegments.cs b/CourseApp/Module4/MinSegments.cs new file mode 100644 index 0000000..696061a --- /dev/null +++ b/CourseApp/Module4/MinSegments.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CourseApp.Module4 +{ + public class MinSegments + { + public static void MinSegmentsMethod() + { + int[] arraySizes = Console.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + int masSize = arraySizes[0]; + int winSize = arraySizes[1]; + int[] arrayElements = Console.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + + Queue mins = new Queue(masSize); + for (int i = 0; i < winSize; i++) + { + while (!mins.Empty() && mins.Back() > arrayElements[i]) + { + mins.PopB(); + } + + mins.PushB(arrayElements[i]); + } + + Console.WriteLine(mins.Front()); + for (int i = winSize; i < masSize; i++) + { + while (!mins.Empty() && mins.Back() > arrayElements[i]) + { + mins.PopB(); + } + + mins.PushB(arrayElements[i]); + if (!mins.Empty() && mins.Front() == arrayElements[i - winSize]) + { + mins.PopF(); + } + + Console.WriteLine(mins.Front()); + } + } + } +} diff --git a/CourseApp/Module4/NearestMin.cs b/CourseApp/Module4/NearestMin.cs new file mode 100644 index 0000000..0c3e409 --- /dev/null +++ b/CourseApp/Module4/NearestMin.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CourseApp.Module4 +{ + public class NearestMin + { + public static void NearestMinMethod() + { + int size = int.Parse(Console.ReadLine()); + int[] numbers = Console.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + int[] res = new int[size]; + Stack n_mins = new Stack(size); + n_mins.Push(numbers[size - 1]); + res[size - 1] = -1; + Stack del = new Stack(size); + del.Push(0); + for (int i = size - 2; i >= 0; i--) + { + int temp = 0; + int buf1 = numbers[i]; + if (buf1 <= n_mins.Peek()) + { + while ((!n_mins.Empty()) ? buf1 <= n_mins.Peek() : false) + { + temp += del.Peek() + 1; + del.Pop(); + n_mins.Pop(); + } + + if (n_mins.Empty()) + { + res[i] = -1; + del.Push(0); + } + else + { + res[i] = i + temp + 1; + } + } + else + { + res[i] = i + 1; + } + + n_mins.Push(buf1); + del.Push(temp); + } + + Console.WriteLine(string.Join(" ", res)); + } + } +} diff --git a/CourseApp/Module4/Queue.cs b/CourseApp/Module4/Queue.cs new file mode 100644 index 0000000..fe61c7a --- /dev/null +++ b/CourseApp/Module4/Queue.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module4 +{ + public class Queue + { + private int[] array; + private int head; + private int tail; + private int size = 0; + + public Queue(int in_size) + { + array = new int[in_size]; + head = in_size / 2; + tail = head - 1; + } + + public void PushF(int input) + { + size++; + head--; + if (head == -1) + { + head = array.Length - 1; + } + + array[head] = input; + } + + public void PushB(int input) + { + size++; + tail++; + if (tail == array.Length) + { + tail = 0; + } + + array[tail] = input; + } + + public void PopF() + { + size--; + head++; + if (head == array.Length) + { + head = 0; + } + } + + public void PopB() + { + size--; + tail--; + if (tail == -1) + { + tail = array.Length - 1; + } + } + + public int Front() + { + return array[head]; + } + + public int Back() + { + return array[tail]; + } + + public bool Empty() + { + return size == 0; + } + + public int Size() + { + return size; + } + + public void Clear() + { + head = array.Length / 2; + tail = head - 1; + } + } +} diff --git a/CourseApp/Module4/Stack.cs b/CourseApp/Module4/Stack.cs new file mode 100644 index 0000000..67fce8d --- /dev/null +++ b/CourseApp/Module4/Stack.cs @@ -0,0 +1,75 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Collections; + +namespace CourseApp.Module4 +{ + public class Stack + { + private readonly int size; + private readonly int[] array; + private int top; + + public Stack(int size) + { + this.size = size; + this.top = 0; + this.array = new int[this.size]; + } + + public int Top + { + get + { + return this.top; + } + } + + public int Size + { + get + { + return this.size; + } + } + + public int Count + { + get + { + return this.top; + } + } + + public bool Full() + { + return this.top == this.size; + } + + public bool Empty() + { + return this.top == 0; + } + + public void Push(int element) + { + if (this.Full()) + { + throw new Exception(); + } + + this.array[this.top++] = element; + } + + public int Peek() + { + return array[this.top - 1]; + } + + public int Pop() + { + return this.array[--this.top]; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module5/BinaryTreeBranches.cs b/CourseApp/Module5/BinaryTreeBranches.cs new file mode 100644 index 0000000..fb1195a --- /dev/null +++ b/CourseApp/Module5/BinaryTreeBranches.cs @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module5 +{ + public class BinaryTreeBranches + { + public class Binary_Tree + { + private Node root; + private List size; + + public static void BinaryTreeBranchesMethod() + { + string s = Console.ReadLine(); + + string[] sValues = s.Split(' '); + + var tree = new Binary_Tree(); + + for (int i = 0; i < sValues.Length - 1; i++) + { + tree.Insert(int.Parse(sValues[i])); + } + + tree.FindLastElem(); + } + + public void Insert(int v) + { + root = InnerInsert(v, root); + } + + public List GetValues() + { + size = new List(); + InnerTraversal(root); + return size; + } + + private bool InnerFind(int v, Node root) + { + if (root == null) + { + return false; + } + + if (v == root.Data) + { + return true; + } + else if (v < root.Data) + { + return InnerFind(v, root.Left); + } + else + { + return InnerFind(v, root.Right); + } + } + + private bool Find(int v) + { + return InnerFind(v, root); + } + + private void FindLastElem() + { + LastElem(root); + } + + private void LastElem(Node value) + { + if (value == null) + { + return; + } + + LastElem(value.Left); + + if ((value.Left != null && value.Right == null) || (value.Right != null && value.Left == null)) + { + Console.WriteLine(value.Data); + } + + LastElem(value.Right); + } + + private Node InnerInsert(int v, Node root) + { + if (root == null) + { + return new Node(v); + } + + if (root.Data > v) + { + root.Left = InnerInsert(v, root.Left); + } + else if (root.Data < v) + { + root.Right = InnerInsert(v, root.Right); + } + + return root; + } + + private void InnerTraversal(Node node) + { + if (node == null) + { + return; + } + + InnerTraversal(node.Left); + size.Add(node.Data); + InnerTraversal(node.Right); + } + + internal class Node + { + public Node(int v) + { + Data = v; + Left = null; + Right = null; + } + + public Node Left { get; set; } + + public Node Right { get; set; } + + public int Data { get; set; } + } + } + } +} diff --git a/CourseApp/Module5/TreeBalanceCheck.cs b/CourseApp/Module5/TreeBalanceCheck.cs new file mode 100644 index 0000000..b13081c --- /dev/null +++ b/CourseApp/Module5/TreeBalanceCheck.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module5 +{ + public class TreeBalanceCheck + { + public class Binary_Tree + { + private Node root; + + public static void TreeBalanceCheckMethod() + { + string s = Console.ReadLine(); + + string[] sValues = s.Split(' '); + + var tree = new Binary_Tree(); + + for (int i = 0; i < sValues.Length - 1; i++) + { + tree.Insert(int.Parse(sValues[i])); + } + + if (tree.IsBalanced(tree.root)) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + public virtual int Height(Node node) + { + if (node == null) + { + return 0; + } + + return 1 + Math.Max(Height(node.Left), Height(node.Right)); + } + + public virtual bool IsBalanced(Node node) + { + int lh; + + int rh; + + if (node == null) + { + return true; + } + + lh = Height(node.Left); + rh = Height(node.Right); + + if (Math.Abs(lh - rh) <= 1 && IsBalanced(node.Left) + && IsBalanced(node.Right)) + { + return true; + } + + return false; + } + + public void Insert(int v) + { + root = InnerInsert(v, root); + } + + private void PrintStart() + { + PrintOneChild(root); + } + + private void PrintOneChild(Node value) + { + if (value == null) + { + return; + } + + PrintOneChild(value.Left); + + if ((value.Left != null && value.Right == null) || (value.Right != null && value.Left == null)) + { + Console.WriteLine(value.Data); + } + + PrintOneChild(value.Right); + } + + private Node InnerInsert(int v, Node root) + { + if (root == null) + { + return new Node(v); + } + + if (root.Data > v) + { + root.Left = InnerInsert(v, root.Left); + } + else if (root.Data < v) + { + root.Right = InnerInsert(v, root.Right); + } + + return root; + } + + public class Node + { + public Node(int v) + { + Data = v; + Left = null; + Right = null; + } + + public Node Left { get; set; } + + public Node Right { get; set; } + + public int Data { get; set; } + } + } + } +} diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..a3d35f4 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,5 +1,8 @@ using System; using CourseApp.Module2; +using CourseApp.Module3; +using CourseApp.Module4; +using CourseApp.Module5; namespace CourseApp { @@ -7,9 +10,7 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - - Console.WriteLine("Hello World"); + RadixSort.RadixSortMethod(); } } } diff --git a/README.md b/README.md index e58af8a..b6edaef 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ -# Tprogramming_42_2020 +# Algorythm_Sharp_2022 + +Egorov Vitaly Alexeyevich Master branch :) \ No newline at end of file