diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index ae46394..27b2938 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + netcoreapp6 True 1573,1591,1701;1702;1705 false diff --git a/CourseApp.Tests/Module2/BubbleSortTest.cs b/CourseApp.Tests/Module2/BubbleSortTest.cs index 8a9b192..5dec511 100644 --- a/CourseApp.Tests/Module2/BubbleSortTest.cs +++ b/CourseApp.Tests/Module2/BubbleSortTest.cs @@ -1,18 +1,28 @@ -using System; -using System.IO; -using Xunit; -using CourseApp.Module2; - -namespace CourseApp.Tests.Module2 +namespace CourseApp.Tests.Module2 { + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + [Collection("Sequential")] public class BubbleSortTest : IDisposable { - private const string Inp1 = @"7 -5 1 7 3 9 4 1"; + private const string Inp1 = @"4 +4 3 2 1"; + + private const string Out1 = @"3 4 2 1 +3 2 4 1 +3 2 1 4 +2 3 1 4 +2 1 3 4 +1 2 3 4"; - private const string Inp2 = @"3 --10 7 2"; + private const string Inp2 = @"4 +1 2 3 4"; + + 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(); @@ -35,11 +45,13 @@ public void Test1(string input, string expected) Console.SetIn(stringReader); // act - BubbleSort.BubbleSortMethod(); + BubbleSort.BubbleSortMeth(); // 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/InversionSortTest.cs b/CourseApp.Tests/Module2/InversionSortTest.cs new file mode 100644 index 0000000..e074c33 --- /dev/null +++ b/CourseApp.Tests/Module2/InversionSortTest.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class InversionSortTest : IDisposable + { + private const string Inp3 = @"5 +5 4 3 2 1"; + + private const string Out3 = @"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(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 + InversionSort.ClassMain(); + + // 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/MergeSortTest.cs b/CourseApp.Tests/Module2/MergeSortTest.cs new file mode 100644 index 0000000..824cc77 --- /dev/null +++ b/CourseApp.Tests/Module2/MergeSortTest.cs @@ -0,0 +1,62 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [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.ClassMain(); + + // 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/NumbDiffTest.cs b/CourseApp.Tests/Module2/NumbDiffTest.cs new file mode 100644 index 0000000..c4ef849 --- /dev/null +++ b/CourseApp.Tests/Module2/NumbDiffTest.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class NumbDiffTest : 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 + NumbDiff.ClassMain(); + + // 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/RadixSortTest.cs b/CourseApp.Tests/Module2/RadixSortTest.cs new file mode 100644 index 0000000..8322fb4 --- /dev/null +++ b/CourseApp.Tests/Module2/RadixSortTest.cs @@ -0,0 +1,81 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [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.ClassMain(); + + // 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/SortingPairsTest.cs b/CourseApp.Tests/Module2/SortingPairsTest.cs new file mode 100644 index 0000000..6734976 --- /dev/null +++ b/CourseApp.Tests/Module2/SortingPairsTest.cs @@ -0,0 +1,60 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class SortingPairsTest : 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 + SortingPairs.BubbleSortMeth(); + + // 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/WareHouseTest.cs b/CourseApp.Tests/Module2/WareHouseTest.cs new file mode 100644 index 0000000..178272d --- /dev/null +++ b/CourseApp.Tests/Module2/WareHouseTest.cs @@ -0,0 +1,51 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class WarehouseTest : 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 + Warehouse.ClassMain(); + + // 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/CyclicShiftTest.cs b/CourseApp.Tests/Module3/CyclicShiftTest.cs new file mode 100644 index 0000000..94e6628 --- /dev/null +++ b/CourseApp.Tests/Module3/CyclicShiftTest.cs @@ -0,0 +1,51 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class CyclicShiftTest : IDisposable + { + private const string Inp1 = @"a + b"; + + private const string Out1 = @"-1"; + + private const string Inp2 = @"zabcd +abcdz"; + + 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 + CyclicShift.ClassMain(); + + // 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/Module3/CyclicStringTest.cs b/CourseApp.Tests/Module3/CyclicStringTest.cs new file mode 100644 index 0000000..015615d --- /dev/null +++ b/CourseApp.Tests/Module3/CyclicStringTest.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class CyclicStringTest : 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 + CyclicString.ClassMain(); + + // 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/PeriodOfStringTest.cs b/CourseApp.Tests/Module3/PeriodOfStringTest.cs new file mode 100644 index 0000000..22e057b --- /dev/null +++ b/CourseApp.Tests/Module3/PeriodOfStringTest.cs @@ -0,0 +1,54 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class PeriodOfStringTest : IDisposable + { + private const string Inp1 = @"aaaaa"; + + private const string Out1 = @"5"; + + private const string Inp2 = @"abcabcabc"; + + private const string Out2 = @"3"; + + private const string Inp3 = @"abab"; + + 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 + PeriodOfString.ClassMain(); + + // 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/Module3/SearchSubstringTest.cs b/CourseApp.Tests/Module3/SearchSubstringTest.cs new file mode 100644 index 0000000..59d4b69 --- /dev/null +++ b/CourseApp.Tests/Module3/SearchSubstringTest.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class SearchSubstringTest : 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 + SearchSubstring.ClassMain(); + + // 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/MinimaOnSegmentsTest.cs b/CourseApp.Tests/Module4/MinimaOnSegmentsTest.cs new file mode 100644 index 0000000..b4fb40e --- /dev/null +++ b/CourseApp.Tests/Module4/MinimaOnSegmentsTest.cs @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class MinimaOnSegmentsTest : 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 + MinimaOnSegments.ClassMain(); + + // 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/Module4/NearestSmallerNumberTest.cs b/CourseApp.Tests/Module4/NearestSmallerNumberTest.cs new file mode 100644 index 0000000..2d3142b --- /dev/null +++ b/CourseApp.Tests/Module4/NearestSmallerNumberTest.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using Xunit; + + [Collection("Sequential")] + public class NearestSmallerNumberTest : 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 + NearestSmallerNumber.ClassMain(); + + // 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/PSPTest.cs b/CourseApp.Tests/Module4/PSPTest.cs new file mode 100644 index 0000000..89d8689 --- /dev/null +++ b/CourseApp.Tests/Module4/PSPTest.cs @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class PSPTest : IDisposable + { + private const string Inp1 = @"())(()"; + + private const string Out1 = @"2"; + + private const string Inp2 = @"))((("; + + 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 + PSP.GETPSP(); + + // 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/SortingTest.cs b/CourseApp.Tests/Module4/SortingTest.cs new file mode 100644 index 0000000..5cca43c --- /dev/null +++ b/CourseApp.Tests/Module4/SortingTest.cs @@ -0,0 +1,57 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class SortingTest : IDisposable + { + private const string Inp1 = @"3 +3 2 1"; + + private const string Out1 = @"YES"; + + private const string Inp2 = @"4 +4 1 3 2"; + + private const string Out2 = @"YES"; + + private const string Inp3 = @"3 +2 3 1"; + + 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 + Sorting.СlassMain(); + + // 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/BalancedTreeTest.cs b/CourseApp.Tests/Module5/BalancedTreeTest.cs new file mode 100644 index 0000000..3f16416 --- /dev/null +++ b/CourseApp.Tests/Module5/BalancedTreeTest.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.IO; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class BalancedTreeTest : 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 + BalancedTree.ClassMain(); + + // 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/BranchesOfaBinarytreeTest.cs b/CourseApp.Tests/Module5/BranchesOfaBinarytreeTest.cs new file mode 100644 index 0000000..20c7deb --- /dev/null +++ b/CourseApp.Tests/Module5/BranchesOfaBinarytreeTest.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.IO; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class BranchesOfaBinaryTreeTest : 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 + BranchesOfaBinaryTree.ClassMain(); + + // 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..5b415d8 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp2.1 + netcoreapp6 True 1573,1591,1701;1702;1705; diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..5e28988 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -6,33 +6,36 @@ namespace CourseApp.Module2 { public class BubbleSort { - public static void BubbleSortMethod() + public static void BubbleSortMeth() { - int n = int.Parse(Console.ReadLine()); + int a = 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[] array = new int[a]; + for (int i = 0; i < a; i++) { - arr[i] = int.Parse(sValues[i]); + array[i] = int.Parse(sValues[i]); } - for (int i = 0; i < arr.Length - 1; i++) + bool swap = false; + for (int i = 0; i < array.Length - 1; i++) { - for (int j = 0; j < arr.Length - i - 1; j++) + for (int 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]); + string result = string.Join(" ", array); + Console.WriteLine(result); + swap = true; } } } - string result = string.Join(" ", arr); - Console.WriteLine(result); + if (swap == false) + { + Console.WriteLine(0); + } } } -} +} \ No newline at end of file diff --git a/CourseApp/Module2/InversionSort.cs b/CourseApp/Module2/InversionSort.cs new file mode 100644 index 0000000..2c7435c --- /dev/null +++ b/CourseApp/Module2/InversionSort.cs @@ -0,0 +1,77 @@ +using System; + +namespace CourseApp.Module2 +{ + public class InversionSort + { + private static long inversion = 0; + + public static int[] Merge(int[] a, int[] b) + { + int i = 0; + int g = 0; + int[] c = new int[a.Length + b.Length]; + for (int k = 0; k < c.Length; k++) + { + if (i == a.Length) + { + c[k] = b[g]; + g++; + } + else if (g == b.Length) + { + c[k] = a[i]; + i++; + } + else if (a[i] <= b[g]) + { + c[k] = a[i]; + i++; + } + else + { + c[k] = b[g]; + g++; + inversion += a.Length - i; + } + } + + return c; + } + + public static int[] Merge_sort(int[] v, int lft, int rght) + { + if (rght - lft == 1) + { + int[] res = new int[1]; + res[0] = v[lft]; + return res; + } + + int m = (lft + rght) / 2; + + int[] left = Merge_sort(v, lft, m); + int[] right = Merge_sort(v, m, rght); + + int[] sort = Merge(left, right); + + return sort; + } + + public static void ClassMain() + { + 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]); + } + + int[] v_sorted = Merge_sort(arr, 0, n); + + Console.WriteLine(inversion); + } + } +} diff --git a/CourseApp/Module2/MergeSort.cs b/CourseApp/Module2/MergeSort.cs new file mode 100644 index 0000000..b299bfa --- /dev/null +++ b/CourseApp/Module2/MergeSort.cs @@ -0,0 +1,76 @@ +using System; + +namespace CourseApp.Module2 +{ + public class MergeSort + { + public static int[] MergSrt(int[] a, int[] b) + { + int i = 0; + int g = 0; + int[] y = new int[a.Length + b.Length]; + for (int k = 0; k < y.Length; k++) + { + if (i == a.Length) + { + y[k] = b[g]; + g++; + } + else if (g == b.Length) + { + y[k] = a[i]; + i++; + } + else if (a[i] <= b[g]) + { + y[k] = a[i]; + i++; + } + else + { + y[k] = b[g]; + g++; + } + } + + return y; + } + + public static int[] MergeSrt1(int[] v, int l, int r) + { + if (r - l == 1) + { + int[] result = new int[1]; + result[0] = v[l]; + return result; + } + + int m = (l + r) / 2; + + int[] left = MergeSrt1(v, l, m); + int[] right = MergeSrt1(v, m, r); + + int[] sort = MergSrt(left, right); + + Console.WriteLine("{0} {1} {2} {3}", l + 1, r, sort[0], sort[^1]); + + return MergSrt(left, right); + } + + public static void ClassMain() + { + 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]); + } + + int[] v_sorted = MergeSrt1(arr, 0, n); + + Console.WriteLine("{0}", string.Join(" ", v_sorted)); + } + } +} diff --git a/CourseApp/Module2/NumbDiff.cs b/CourseApp/Module2/NumbDiff.cs new file mode 100644 index 0000000..9525810 --- /dev/null +++ b/CourseApp/Module2/NumbDiff.cs @@ -0,0 +1,74 @@ +using System; + +namespace CourseApp.Module2 +{ + public class NumbDiff + { + private static long count = 1; + + public static int Partition(int[] arr, int left, int right) + { + int p = arr[left]; + int i = left - 1, g = right + 1; + + while (true) + { + do + { + i++; + } + while (arr[i] < p); + + do + { + g--; + } + while (arr[g] > p); + + if (i >= g) + { + return g; + } + + int temp = arr[i]; + arr[i] = arr[g]; + arr[g] = temp; + } + } + + public static void QuickSort(int[] arr, int left, int right) + { + if (left < right) + { + int q = Partition(arr, left, right); + + QuickSort(arr, left, q); + QuickSort(arr, q + 1, right); + } + } + + public static void ClassMain() + { + 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); + } + } +} diff --git a/CourseApp/Module2/RadixSort.cs b/CourseApp/Module2/RadixSort.cs new file mode 100644 index 0000000..7d71a51 --- /dev/null +++ b/CourseApp/Module2/RadixSort.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace CourseApp.Module2 +{ + public class RadixSort + { + public static string[] CountSort(string[] arr_str, int phase, int lenght) + { + ulong i; + List[] arr_list = new List[10]; + for (int g = 0; g < 10; g++) + { + arr_list[g] = new List(); + } + + for (int j = 0; j < arr_str.Length; j++) + { + int k = int.Parse(arr_str[j].Substring(lenght - phase, 1)); + arr_list[k].Add(arr_str[j]); + } + + for (i = 0; i < 10; i++) + { + if (arr_list[i].Count == 0) + { + Console.WriteLine("Bucket " + i + ": empty"); + } + else + { + Console.WriteLine("Bucket " + i + ": {0}", string.Join(", ", arr_list[i])); + } + } + + int l = 0; + + for (i = 0; i < 10; i++) + { + for (int j = 0; j < arr_list[i].Count; j++) + { + arr_str[l] = arr_list[i][j]; + l++; + } + } + + return arr_str; + } + + public static void Radixsort(string[] arr_str, ulong n) + { + int numb_phaze = 1; + int rank = arr_str[0].Length; + + Console.WriteLine("Initial array:"); + Console.WriteLine("{0}", string.Join(", ", arr_str)); + + foreach (var i in Enumerable.Range(0, Convert.ToInt32(Math.Ceiling(Convert.ToDouble(-1 - (rank - 1)) / -1))).Select(x_1 => rank - 1 + (x_1 * -1))) + { + Console.WriteLine("**********"); + Console.WriteLine("Phase {0}", numb_phaze); + arr_str = CountSort(arr_str, numb_phaze, rank); + numb_phaze++; + } + + Console.WriteLine("**********"); + Console.WriteLine("Sorted array:"); + Console.Write("{0}", string.Join(", ", arr_str)); + } + + public static void ClassMain() + { + ulong n = ulong.Parse(Console.ReadLine()); + string[] arr_str = new string[n]; + for (ulong i = 0; i < n; i++) + { + arr_str[i] = Console.ReadLine(); + } + + Radixsort(arr_str, n); + } + } +} diff --git a/CourseApp/Module2/SortingPairs.cs b/CourseApp/Module2/SortingPairs.cs new file mode 100644 index 0000000..c98c48e --- /dev/null +++ b/CourseApp/Module2/SortingPairs.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class SortingPairs + { + public static void BubbleSortMeth() + { + int n = int.Parse(Console.ReadLine()); + int[,] pair = new int[n, 2]; + for (int i = 0; i < n; i++) + { + string a = Console.ReadLine(); + string[] sValues = a.Split(' '); + pair[i, 0] = int.Parse(sValues[0]); + pair[i, 1] = int.Parse(sValues[1]); + } + + for (int i = 0; i < (pair.Length / 2) - 1; i++) + { + for (int j = 0; j < (pair.Length / 2) - i - 1; j++) + { + if (pair[j, 1] < pair[j + 1, 1]) + { + (pair[j, 1], pair[j + 1, 1]) = (pair[j + 1, 1], pair[j, 1]); + (pair[j, 0], pair[j + 1, 0]) = (pair[j + 1, 0], pair[j, 0]); + } + else if (pair[j, 1] == pair[j + 1, 1]) + { + if (pair[j, 0] > pair[j + 1, 0]) + { + (pair[j, 0], pair[j + 1, 0]) = (pair[j + 1, 0], pair[j, 0]); + (pair[j, 1], pair[j + 1, 1]) = (pair[j + 1, 1], pair[j, 1]); + } + } + } + } + + for (int i = 0; i < n; i++) + { + Console.WriteLine("{0} {1}", pair[i, 0], pair[i, 1]); + } + } + } +} diff --git a/CourseApp/Module2/WareHouse.cs b/CourseApp/Module2/WareHouse.cs new file mode 100644 index 0000000..601fa38 --- /dev/null +++ b/CourseApp/Module2/WareHouse.cs @@ -0,0 +1,61 @@ +using System; + +namespace CourseApp.Module2 +{ + public class Warehouse + { + public static void Calculate(int[] arr_of_order, int[] arr_of_products, int a) + { + int[] c = new int[a + 1]; + for (int g = 0; g < arr_of_order.Length; g++) + { + c[arr_of_order[g]]++; + } + + int pos = 0; + int index = 0; + int[] order = new int[a]; + string[] answer = new string[a]; + for (int g = 0; g < c.Length; g++) + { + if (c[g] != 0) + { + order[pos++] = c[g]; + if (arr_of_products[index] >= order[index]) + { + Console.WriteLine("no"); + } + else + { + Console.WriteLine("yes"); + } + + index++; + } + } + } + + public static void ClassMain() + { + int products = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + int[] arr_of_products = new int[products]; + for (int g = 0; g < products; g++) + { + arr_of_products[g] = int.Parse(sValues[g]); + } + + int n = int.Parse(Console.ReadLine()); + s = Console.ReadLine(); + sValues = s.Split(' '); + int[] arr_of_order = new int[n]; + for (int g = 0; g < n; g++) + { + arr_of_order[g] = int.Parse(sValues[g]); + } + + Calculate(arr_of_order, arr_of_products, products); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/CyclicShift.cs b/CourseApp/Module3/CyclicShift.cs new file mode 100644 index 0000000..b20da16 --- /dev/null +++ b/CourseApp/Module3/CyclicShift.cs @@ -0,0 +1,74 @@ +using System; +using System.Linq; + +namespace CourseApp.Module3 +{ + public class CyclicShift + { + public static int Rabin_Karp(string s, string t) + { + if (s == t) + { + return 0; + } + + t = string.Concat(Enumerable.Repeat(t, 2)); + + long q = 13; + long m = 1; + long p = 4999900 - 12; + + long first_hash = 0; + long second_hash = 0; + long xt = 1; + + foreach (char g in s.Reverse()) + { + first_hash = (first_hash + (g * m)) % p; + m = (m * q) % p; + } + + m = 1; + + for (int g = s.Length - 1; g >= 0; g--) + { + second_hash = (second_hash + (t[g] * m)) % p; + m = (m * q) % p; + } + + for (int g = 0; g < s.Length - 1; g++) + { + xt = (xt * q) % p; + } + + for (int g = 1; g < t.Length - s.Length + 1; g++) + { + if (first_hash == second_hash) + { + return g - 1; + } + + second_hash = q * (second_hash - (t[g - 1] * xt)); + second_hash += t[g + s.Length - 1]; + second_hash = second_hash % p; + + if ((second_hash < 0 && p > 0) || (second_hash > 0 && p < 0)) + { + second_hash += p; + } + } + + return -1; + } + + public static void ClassMain() + { + string s = Console.ReadLine(); + string t = Console.ReadLine(); + + int result = Rabin_Karp(s, t); + + Console.WriteLine(result); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/CyclicString.cs b/CourseApp/Module3/CyclicString.cs new file mode 100644 index 0000000..c280f24 --- /dev/null +++ b/CourseApp/Module3/CyclicString.cs @@ -0,0 +1,45 @@ +using System; + +namespace CourseApp.Module3 +{ + public class CyclicString + { + public static int[] PrfxFunction(string k) + { + int[] result = new int[k.Length]; + result[0] = 0; + + for (int g = 0; g < k.Length - 1; g++) + { + int j = result[g]; + + while (j > 0 && k[g + 1] != k[j]) + { + j = result[j - 1]; + } + + if (k[g + 1] == k[j]) + { + result[g + 1] = j + 1; + } + else + { + result[g + 1] = 0; + } + } + + return result; + } + + public static void ClassMain() + { + string k = Console.ReadLine(); + + int[] prefixK = PrfxFunction(k); + + int result = k.Length - prefixK[k.Length - 1]; + + Console.WriteLine(result); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/PeriodOfString.cs b/CourseApp/Module3/PeriodOfString.cs new file mode 100644 index 0000000..8ccb4dc --- /dev/null +++ b/CourseApp/Module3/PeriodOfString.cs @@ -0,0 +1,52 @@ +using System; + +namespace CourseApp.Module3 +{ + public class PeriodOfString + { + public static int[] PrfxFunction(string k) + { + int[] result = new int[k.Length]; + result[0] = 0; + + for (int g = 0; g < k.Length - 1; g++) + { + int j = result[g]; + + while (j > 0 && k[g + 1] != k[j]) + { + j = result[j - 1]; + } + + if (k[g + 1] == k[j]) + { + result[g + 1] = j + 1; + } + else + { + result[g + 1] = 0; + } + } + + return result; + } + + public static void ClassMain() + { + string k = Console.ReadLine(); + + int[] prefixK = PrfxFunction(k); + + int result = k.Length - prefixK[k.Length - 1]; + + if (k.Length % result == 0) + { + Console.WriteLine(k.Length / result); + } + else + { + Console.WriteLine(1); + } + } + } +} diff --git a/CourseApp/Module3/SearchSubstring.cs b/CourseApp/Module3/SearchSubstring.cs new file mode 100644 index 0000000..8e6f2d1 --- /dev/null +++ b/CourseApp/Module3/SearchSubstring.cs @@ -0,0 +1,57 @@ +using System; + +namespace CourseApp.Module3 +{ + public class SearchSubstring + { + 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 ClassMain() + { + string s = Console.ReadLine(); + string t = Console.ReadLine(); + + int p = 9999999 + 7; + int x = 26; + + Rabin_karp(s, t, p, x); + } + } +} diff --git a/CourseApp/Module4/MinimaOnSegments.cs b/CourseApp/Module4/MinimaOnSegments.cs new file mode 100644 index 0000000..f689940 --- /dev/null +++ b/CourseApp/Module4/MinimaOnSegments.cs @@ -0,0 +1,137 @@ +using System; + +public class MinimaOnSegments +{ + public static void ClassMain() + { + string s1 = Console.ReadLine(); + string[] s1Values = s1.Split(' '); + int[] array1 = new int[2]; + for (int g = 0; g < 2; g++) + { + array1[g] = int.Parse(s1Values[g]); + } + + string s2 = Console.ReadLine(); + string[] s2Values = s2.Split(' '); + int[] array2 = new int[array1[0]]; + for (int g = 0; g < array1[0]; g++) + { + array2[g] = int.Parse(s2Values[g]); + } + + for (int g = 0; g < array1[1]; g++) + { + while (Deque.Empty() == false && array2[g] < array2[Deque.Front()]) + { + Deque.Pop_Front(); + } + + Deque.Push_Front(g); + } + + for (int g = array1[1]; g < array1[0]; g++) + { + Console.WriteLine(array2[Deque.Back()]); + + while (Deque.Empty() == false && Deque.Back() <= g - array1[1]) + { + Deque.Pop_Back(); + } + + while (Deque.Empty() == false && array2[g] < array2[Deque.Front()]) + { + Deque.Pop_Front(); + if (Deque.Size() == 0) + { + Deque.Clear(); + } + } + + Deque.Push_Front(g); + } + + Console.WriteLine(array2[Deque.Back()]); + } + + private class Deque + { + private static int[] buff = new int[100000001]; + private static int front = 0; + private static int back = 100000001 - 1; + private static int size = 0; + + public static void Push_Back(int t) + { + back++; + if (back == 100000001) + { + back = 0; + } + + buff[back] = t; + size++; + } + + public static void Push_Front(int t) + { + front--; + if (front < 0) + { + front = 100000001 - 1; + } + + buff[front] = t; + size++; + } + + public static void Pop_Back() + { + back--; + if (back < 0) + { + back = 100000001 - 1; + } + + size--; + } + + public static void Pop_Front() + { + front++; + if (front == 100000001) + { + front = 0; + } + + size--; + } + + public static void Clear() + { + front = 0; + back = 100000001 - 1; + size = 0; + } + + public static int Front() + { + return buff[front]; + } + + public static int Back() + { + return buff[back]; + } + + public static int Size() + { + return size; + } + + public static bool Empty() + { + return size == 0; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/NearestSmallerNumber.cs b/CourseApp/Module4/NearestSmallerNumber.cs new file mode 100644 index 0000000..26f9eda --- /dev/null +++ b/CourseApp/Module4/NearestSmallerNumber.cs @@ -0,0 +1,81 @@ +using System; + +public class NearestSmallerNumber +{ + public static void ClassMain() + { + var numberElements = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var sValues = s.Split(' '); + var elementOfArray = new int[numberElements]; + var arrayOfAnswer = new int[numberElements]; + for (int g = 0; g < numberElements; g++) + { + elementOfArray[g] = int.Parse(sValues[g]); + } + + int index = numberElements - 1; + while (index >= 0) + { + while (Stack.Empty() == false && elementOfArray[Stack.Back()] >= elementOfArray[index]) + { + Stack.Pop(); + } + + if (Stack.Empty() == true) + { + arrayOfAnswer[index] = -1; + } + else + { + arrayOfAnswer[index] = Stack.Back(); + } + + Stack.Push(index); + + index--; + } + + for (int g = 0; g < numberElements; g++) + { + Console.Write(arrayOfAnswer[g] + " "); + } + } + + private class Stack + { + private static int[] buff = new int[100000001]; + private static int top = -1; + + public static void Push(int a) + { + top++; + buff[top] = a; + } + + public static void Pop() + { + top--; + } + + public static int Size() + { + return top + 1; + } + + public static bool Empty() + { + return top == -1; + } + + public static void Clear() + { + top = -1; + } + + public static int Back() + { + return buff[top]; + } + } +} diff --git a/CourseApp/Module4/PSP.cs b/CourseApp/Module4/PSP.cs new file mode 100644 index 0000000..86af732 --- /dev/null +++ b/CourseApp/Module4/PSP.cs @@ -0,0 +1,68 @@ +using System; + +namespace CourseApp.Module4 +{ + public class PSP + { + public static void GETPSP() + { + string parentheses = Console.ReadLine(); + int counter = 0; + + for (int g = 0; g < parentheses.Length; g++) + { + if (parentheses[g] == '(') + { + Stack.Push(parentheses[g]); + } + else if (Stack.Empty() == false && parentheses[g] == ')') + { + Stack.Pop(); + } + else + { + counter += 1; + } + } + + Console.WriteLine(counter + Stack.Size()); + } + + private class Stack + { + private static int[] buffer = new int[100000001]; + private static int top = -1; + + public static void Push(int a) + { + top++; + buffer[top] = a; + } + + public static void Pop() + { + top--; + } + + public static int Size() + { + return top + 1; + } + + public static bool Empty() + { + return top == -1; + } + + public static void Clear() + { + top = -1; + } + + public static int Back() + { + return buffer[top]; + } + } + } +} diff --git a/CourseApp/Module4/Sorting.cs b/CourseApp/Module4/Sorting.cs new file mode 100644 index 0000000..7c6e924 --- /dev/null +++ b/CourseApp/Module4/Sorting.cs @@ -0,0 +1,94 @@ +using System; + +namespace CourseApp.Module4 +{ + public class Sorting + { + public static void СlassMain() + { + var numberOfWagons = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var sValues = s.Split(' '); + var arrayWagons = new int[numberOfWagons]; + var answerArray = new int[numberOfWagons]; + for (int g = 0; g < numberOfWagons; g++) + { + arrayWagons[g] = int.Parse(sValues[g]); + } + + int value1 = 0; + int value2 = 0; + + while (value1 != numberOfWagons) + { + if (Stack.Empty() == true || (value2 < numberOfWagons && arrayWagons[value2] < Stack.Back())) + { + Stack.Push(arrayWagons[value2]); + value2++; + } + else + { + answerArray[value1] += Stack.Back(); + Stack.Pop(); + value1++; + } + } + + bool isAnswer = true; + + for (int g = 0; g < answerArray.Length - 1; g++) + { + if (answerArray[g] > answerArray[g + 1]) + { + isAnswer = false; + } + } + + if (isAnswer == true) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + private class Stack + { + private static int[] buffer = new int[100000001]; + private static int top = -1; + + public static void Push(int a) + { + top++; + buffer[top] = a; + } + + public static void Pop() + { + top--; + } + + public static int Size() + { + return top + 1; + } + + public static bool Empty() + { + return top == -1; + } + + public static void Clear() + { + top = -1; + } + + public static int Back() + { + return buffer[top]; + } + } + } +} diff --git a/CourseApp/Module5/BalancedTree.cs b/CourseApp/Module5/BalancedTree.cs new file mode 100644 index 0000000..2e28389 --- /dev/null +++ b/CourseApp/Module5/BalancedTree.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module5 +{ + public class BalancedTree + { + private Node root; + + public static void ClassMain() + { + string k = Console.ReadLine(); + + string[] values = k.Split(' '); + + var tree = new BalancedTree(); + + for (int g = 0; g < values.Length - 1; g++) + { + tree.Insert(int.Parse(values[g])); + } + + if (tree.Balanced(tree.root)) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + public void Insert(int data) + { + root = InnerInsert(data, root); + } + + private Node InnerInsert(int data, Node root) + { + if (root == null) + { + return new Node(data); + } + + if (root.Data > data) + { + root.Left = InnerInsert(data, root.Left); + } + else if (root.Data < data) + { + root.Right = InnerInsert(data, root.Right); + } + + return root; + } + + private bool Balanced(Node node) + { + int leftHeight; + + int rightHeight; + + if (node == null) + { + return true; + } + + leftHeight = Height(node.Left); + rightHeight = Height(node.Right); + + if (Math.Abs(leftHeight - rightHeight) <= 1 && Balanced(node.Left) + && Balanced(node.Right)) + { + return true; + } + + return false; + } + + private int Height(Node node) + { + if (node == null) + { + return 0; + } + + return 1 + Math.Max(Height(node.Left), Height(node.Right)); + } + } +} diff --git a/CourseApp/Module5/BranchesOfaBinaryTree.cs b/CourseApp/Module5/BranchesOfaBinaryTree.cs new file mode 100644 index 0000000..4508948 --- /dev/null +++ b/CourseApp/Module5/BranchesOfaBinaryTree.cs @@ -0,0 +1,71 @@ +using System; + +namespace CourseApp.Module5 +{ + public class BranchesOfaBinaryTree + { + private Node root; + + public static void ClassMain() + { + string k = Console.ReadLine(); + + string[] values = k.Split(' '); + + var tree = new BranchesOfaBinaryTree(); + + for (int g = 0; g < values.Length - 1; g++) + { + tree.Insert(int.Parse(values[g])); + } + + tree.LaunchPrintLastNodes(); + } + + public void Insert(int data) + { + root = InnerInsert(data, root); + } + + private void LaunchPrintLastNodes() + { + PrintLastNodes(root); + } + + private void PrintLastNodes(Node value) + { + if (value == null) + { + return; + } + + PrintLastNodes(value.Left); + + if ((value.Left != null && value.Right == null) || (value.Right != null && value.Left == null)) + { + Console.WriteLine(value.Data); + } + + PrintLastNodes(value.Right); + } + + private Node InnerInsert(int data, Node root) + { + if (root == null) + { + return new Node(data); + } + + if (root.Data > data) + { + root.Left = InnerInsert(data, root.Left); + } + else if (root.Data < data) + { + root.Right = InnerInsert(data, root.Right); + } + + return root; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module5/Node.cs b/CourseApp/Module5/Node.cs new file mode 100644 index 0000000..eaf5bcc --- /dev/null +++ b/CourseApp/Module5/Node.cs @@ -0,0 +1,18 @@ +namespace CourseApp.Module5 +{ + public class Node + { + public Node(int data) + { + Data = data; + Left = null; + Right = null; + } + + public Node Left { get; set; } + + public Node Right { get; set; } + + public int Data { get; set; } + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..bbe301d 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,5 +1,7 @@ -using System; -using CourseApp.Module2; +using CourseApp.Module2; +using CourseApp.Module3; +using CourseApp.Module4; +using CourseApp.Module5; namespace CourseApp { @@ -7,9 +9,22 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - - Console.WriteLine("Hello World"); + /*BubbleSort.BubbleSortMeth();*/ + /*SortingPairs.BubbleSortMeth();*/ + /*MergeSort.ClassMain();*/ + /*NumbDiff.ClassMain();*/ + /*InversionSort.ClassMain();*/ + /*Warehouse.ClassMain();*/ + /*RadixSort.ClassMain();*/ + /*SearchSubstring.ClassMain();*/ + /*PeriodOfString.ClassMain();*/ + /*CyclicString.ClassMain();*/ + /*PSP.GETPSP();*/ + /*Sorting.СlassMain();*/ + /*NearestSmallerNumber.ClassMain();*/ + /*MinimaOnSegments.ClassMain();*/ + /*BranchesOfaBinaryTree.ClassMain();*/ + BalancedTree.ClassMain(); } } } diff --git a/README.md b/README.md index e58af8a..2f970ea 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Tprogramming_42_2020 - -Master branch :) \ No newline at end of file +Gerasimov Danila Sergeevich +Master branch :)