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/BitwiseSortTest.cs b/CourseApp.Tests/Module2/BitwiseSortTest.cs new file mode 100644 index 0000000..4374a92 --- /dev/null +++ b/CourseApp.Tests/Module2/BitwiseSortTest.cs @@ -0,0 +1,82 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class BitwiseSortTest : 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 + BitwiseSort.Try(); + + // 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/BubbleSortPTest.cs b/CourseApp.Tests/Module2/BubbleSortPTest.cs new file mode 100644 index 0000000..a62b438 --- /dev/null +++ b/CourseApp.Tests/Module2/BubbleSortPTest.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 BubbleSortPTest : 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 + BubbleSortP.BubbleSortM(); + + // 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..41c6082 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.BubbleSortM(); // 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/Count_DifTest.cs b/CourseApp.Tests/Module2/Count_DifTest.cs new file mode 100644 index 0000000..ab57bfd --- /dev/null +++ b/CourseApp.Tests/Module2/Count_DifTest.cs @@ -0,0 +1,46 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class Count_DifTest : 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 + Count_Dif.Try(); + + // 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/Invers_SortTest.cs b/CourseApp.Tests/Module2/Invers_SortTest.cs new file mode 100644 index 0000000..cbc4724 --- /dev/null +++ b/CourseApp.Tests/Module2/Invers_SortTest.cs @@ -0,0 +1,46 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class Invers_SortTest : IDisposable + { + private const string Inp1 = @"2 +3 1"; + + 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 + Invers_Sort.Try(); + + // 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/Sort_MergeTest.cs b/CourseApp.Tests/Module2/Sort_MergeTest.cs new file mode 100644 index 0000000..b355741 --- /dev/null +++ b/CourseApp.Tests/Module2/Sort_MergeTest.cs @@ -0,0 +1,63 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class Sort_MergeTest : 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 + Sort_Merge.Try(); + + // 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/Storage_SortTest.cs b/CourseApp.Tests/Module2/Storage_SortTest.cs new file mode 100644 index 0000000..ca809be --- /dev/null +++ b/CourseApp.Tests/Module2/Storage_SortTest.cs @@ -0,0 +1,52 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class Storage_SortTest : 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 + Storage_Sort.Try(); + + // 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/CycShiftTest.cs b/CourseApp.Tests/Module3/CycShiftTest.cs new file mode 100644 index 0000000..d499a99 --- /dev/null +++ b/CourseApp.Tests/Module3/CycShiftTest.cs @@ -0,0 +1,51 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class CycShiftTest : 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 + CycShift.EnterValues(); + + // 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/CycStrTest.cs b/CourseApp.Tests/Module3/CycStrTest.cs new file mode 100644 index 0000000..8921fd8 --- /dev/null +++ b/CourseApp.Tests/Module3/CycStrTest.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class CycStrTest : 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 + CycStr.EnterValues(); + + // 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/LinePerTest.cs b/CourseApp.Tests/Module3/LinePerTest.cs new file mode 100644 index 0000000..5b60722 --- /dev/null +++ b/CourseApp.Tests/Module3/LinePerTest.cs @@ -0,0 +1,54 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class LinePerTest : 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 + LinePer.EnterValues(); + + // 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..0e41a63 --- /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.EnterValues(); + + // 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/Correct_bracket_sequenceTest.cs b/CourseApp.Tests/Module4/Correct_bracket_sequenceTest.cs new file mode 100644 index 0000000..c21ad25 --- /dev/null +++ b/CourseApp.Tests/Module4/Correct_bracket_sequenceTest.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class Correct_bracket_sequenceTest : IDisposable + { + private const string Inp1 = @"))((("; + + private const string Out1 = @"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)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + Correct_bracket_sequence.CBS_Method(); + + // 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/MinOnSegTest.cs b/CourseApp.Tests/Module4/MinOnSegTest.cs new file mode 100644 index 0000000..8f74592 --- /dev/null +++ b/CourseApp.Tests/Module4/MinOnSegTest.cs @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class MinOnSegTest : 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 + MinOnSeg.MinOnSeg_Method(); + + // 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/NearSmallerTest.cs b/CourseApp.Tests/Module4/NearSmallerTest.cs new file mode 100644 index 0000000..e779ff8 --- /dev/null +++ b/CourseApp.Tests/Module4/NearSmallerTest.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class NearSmallerTest : 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 + NearSmaller.NearSmaller_Method(); + + // 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/SortTest.cs b/CourseApp.Tests/Module4/SortTest.cs new file mode 100644 index 0000000..dbb818a --- /dev/null +++ b/CourseApp.Tests/Module4/SortTest.cs @@ -0,0 +1,58 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class SortTest : 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 + Sort.Sort_Method(); + + // 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/Balanced_TreeTest.cs b/CourseApp.Tests/Module5/Balanced_TreeTest.cs new file mode 100644 index 0000000..4da0474 --- /dev/null +++ b/CourseApp.Tests/Module5/Balanced_TreeTest.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.IO; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class Balanced_TreeTest : 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 + Balanced_Tree.BalTreeMeth(); + + // 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/Bin_TreeTest.cs b/CourseApp.Tests/Module5/Bin_TreeTest.cs new file mode 100644 index 0000000..8168d2f --- /dev/null +++ b/CourseApp.Tests/Module5/Bin_TreeTest.cs @@ -0,0 +1,46 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class Bin_TreeTest : 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 + Bin_Tree.BinTreeMeth(); + + // 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/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/BitwiseSort.cs b/CourseApp/Module2/BitwiseSort.cs new file mode 100644 index 0000000..6e36882 --- /dev/null +++ b/CourseApp/Module2/BitwiseSort.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class BitwiseSort + { + public static string[] CountSort(string[] arr_string, int phase, int len) + { + ulong i; + List[] arrayList = new List[10]; + for (int g = 0; g < 10; g++) + { + arrayList[g] = new List(); + } + + for (int j = 0; j < arr_string.Length; j++) + { + int k = int.Parse(arr_string[j].Substring(len - phase, 1)); + arrayList[k].Add(arr_string[j]); + } + + for (i = 0; i < 10; i++) + { + if (arrayList[i].Count == 0) + { + Console.WriteLine("Bucket " + i + ": empty"); + } + else + { + Console.WriteLine("Bucket " + i + ": {0}", string.Join(", ", arrayList[i])); + } + } + + int l = 0; + + for (i = 0; i < 10; i++) + { + for (int j = 0; j < arrayList[i].Count; j++) + { + arr_string[l] = arrayList[i][j]; + l++; + } + } + + return arr_string; + } + + public static void Radixsort(string[] arr_string, ulong n) + { + int numb_phaze = 1; + int rank = arr_string[0].Length; + + Console.WriteLine("Initial array:"); + Console.WriteLine("{0}", string.Join(", ", arr_string)); + + 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_string = CountSort(arr_string, numb_phaze, rank); + numb_phaze++; + } + + Console.WriteLine("**********"); + Console.WriteLine("Sorted array:"); + Console.Write("{0}", string.Join(", ", arr_string)); + } + + public static void Method() + { + ulong n = ulong.Parse(Console.ReadLine()); + string[] arr_string = new string[n]; + for (ulong i = 0; i < n; i++) + { + arr_string[i] = Console.ReadLine(); + } + + Radixsort(arr_string, n); + } + } +} diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..798c578 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 BubbleSortM() { int n = int.Parse(Console.ReadLine()); string s = Console.ReadLine(); string[] sValues = s.Split(' '); - int[] arr = new int[n]; + int[] array = new int[n]; for (int i = 0; i < n; i++) { - arr[i] = int.Parse(sValues[i]); + array[i] = int.Parse(sValues[i]); } - for (int i = 0; i < arr.Length - 1; i++) + bool swaping = 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]); + swaping = true; + (array[j], array[j + 1]) = (array[j + 1], array[j]); + string resultate = string.Join(" ", array); + Console.WriteLine(resultate); } } } - string result = string.Join(" ", arr); - Console.WriteLine(result); + if (swaping == false) + { + Console.WriteLine(0); + } } } } diff --git a/CourseApp/Module2/BubbleSortP.cs b/CourseApp/Module2/BubbleSortP.cs new file mode 100644 index 0000000..70ebd0e --- /dev/null +++ b/CourseApp/Module2/BubbleSortP.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class BubbleSortP + { + public static void BubbleSortM() + { + int n = int.Parse(Console.ReadLine()); + int[,] mass = new int[n, 2]; + for (int i = 0; i < n; i++) + { + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + mass[i, 0] = int.Parse(sValues[0]); + mass[i, 1] = int.Parse(sValues[1]); + } + + for (int i = 0; i < (mass.Length / 2) - 1; i++) + { + for (int j = 0; j < (mass.Length / 2) - i - 1; j++) + { + if (mass[j, 1] < mass[j + 1, 1]) + { + (mass[j, 1], mass[j + 1, 1]) = (mass[j + 1, 1], mass[j, 1]); + (mass[j, 0], mass[j + 1, 0]) = (mass[j + 1, 0], mass[j, 0]); + } + else if (mass[j, 1] == mass[j + 1, 1]) + { + if (mass[j, 0] > mass[j + 1, 0]) + { + (mass[j, 0], mass[j + 1, 0]) = (mass[j + 1, 0], mass[j, 0]); + (mass[j, 1], mass[j + 1, 1]) = (mass[j + 1, 1], mass[j, 1]); + } + } + } + } + + for (int i = 0; i < n; i++) + { + Console.WriteLine("{0} {1}", mass[i, 0], mass[i, 1]); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/Count_Dif.cs b/CourseApp/Module2/Count_Dif.cs new file mode 100644 index 0000000..ce11509 --- /dev/null +++ b/CourseApp/Module2/Count_Dif.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class Count_Dif + { + private static long count = 1; + + 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 Quick_Sort(int[] arr, int l, int r) + { + if (l < r) + { + int q = Partition(arr, l, r); + + Quick_Sort(arr, l, q); + Quick_Sort(arr, q + 1, r); + } + } + + public static void Try() + { + 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]); + } + + Quick_Sort(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/Invers_Sort.cs b/CourseApp/Module2/Invers_Sort.cs new file mode 100644 index 0000000..e41cd16 --- /dev/null +++ b/CourseApp/Module2/Invers_Sort.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class Invers_Sort + { + private static long inversion = 0; + + public static int[] Merge_Sort(int[] a, int[] b) + { + int i = 0; + int j = 0; + int[] c = new int[a.Length + b.Length]; + for (int k = 0; k < c.Length; k++) + { + if (i == a.Length) + { + c[k] = b[j]; + j++; + } + else if (j == b.Length) + { + c[k] = a[i]; + i++; + } + else if (a[i] <= b[j]) + { + c[k] = a[i]; + i++; + } + else + { + c[k] = b[j]; + j++; + inversion += a.Length - i; + } + } + + return c; + } + + public static int[] Merge_sort_2(int[] v, int l, int r) + { + if (r - l == 1) + { + int[] res = new int[1]; + res[0] = v[l]; + return res; + } + + int m = (l + r) / 2; + + int[] left = Merge_sort_2(v, l, m); + int[] right = Merge_sort_2(v, m, r); + + int[] sort = Merge_Sort(left, right); + + return sort; + } + + public static void Try() + { + 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_2(arr, 0, n); + + Console.WriteLine(inversion); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/Sort_Merge.cs b/CourseApp/Module2/Sort_Merge.cs new file mode 100644 index 0000000..9e1d9fa --- /dev/null +++ b/CourseApp/Module2/Sort_Merge.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class Sort_Merge + { + public static int[] Merge_Sort(int[] a, int[] b) + { + int i = 0; + int j = 0; + int[] c = new int[a.Length + b.Length]; + for (int k = 0; k < c.Length; k++) + { + if (i == a.Length) + { + c[k] = b[j]; + j++; + } + else if (j == b.Length) + { + c[k] = a[i]; + i++; + } + else if (a[i] <= b[j]) + { + c[k] = a[i]; + i++; + } + else + { + c[k] = b[j]; + j++; + } + } + + return c; + } + + public static int[] Merge_sort_2(int[] v, int l, int r) + { + if (r - l == 1) + { + int[] res = new int[1]; + res[0] = v[l]; + return res; + } + + int m = (l + r) / 2; + + int[] left = Merge_sort_2(v, l, m); + int[] right = Merge_sort_2(v, m, r); + + int[] sort = Merge_Sort(left, right); + + Console.WriteLine("{0} {1} {2} {3}", l + 1, r, sort[0], sort[^1]); + + return Merge_Sort(left, right); + } + + public static void Try() + { + 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_2(arr, 0, n); + + Console.WriteLine("{0}", string.Join(" ", v_sorted)); + } + } +} diff --git a/CourseApp/Module2/Storage_Sort.cs b/CourseApp/Module2/Storage_Sort.cs new file mode 100644 index 0000000..682ed6d --- /dev/null +++ b/CourseApp/Module2/Storage_Sort.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class Storage_Sort + { + public static void Num_sort(int[] order_arr, int[] products_arr, int k) + { + int[] c = new int[k + 1]; + for (int i = 0; i < order_arr.Length; i++) + { + c[order_arr[i]]++; + } + + int pos = 0; + int kekw = 0; + int[] order = new int[k]; + string[] answer = new string[k]; + for (int i = 0; i < c.Length; i++) + { + if (c[i] != 0) + { + order[pos++] = c[i]; + if (products_arr[kekw] >= order[kekw]) + { + Console.WriteLine("no"); + } + else + { + Console.WriteLine("yes"); + } + + kekw++; + } + } + } + + public static void Try() + { + int products = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + int[] products_arr = new int[products]; + for (int i = 0; i < products; i++) + { + products_arr[i] = int.Parse(sValues[i]); + } + + int n = int.Parse(Console.ReadLine()); + s = Console.ReadLine(); + sValues = s.Split(' '); + int[] order_arr = new int[n]; + for (int i = 0; i < n; i++) + { + order_arr[i] = int.Parse(sValues[i]); + } + + Num_sort(order_arr, products_arr, products); + } + } +} diff --git a/CourseApp/Module3/CycShift.cs b/CourseApp/Module3/CycShift.cs new file mode 100644 index 0000000..7bf1b6c --- /dev/null +++ b/CourseApp/Module3/CycShift.cs @@ -0,0 +1,74 @@ +using System; +using System.Linq; + +namespace CourseApp.Module3 +{ + public class CycShift + { + 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 = 7878980; + + long first_hash = 0; + long second_hash = 0; + long xt = 1; + + foreach (char i in s.Reverse()) + { + first_hash = (first_hash + (i * m)) % p; + m = (m * q) % p; + } + + m = 1; + + for (int i = s.Length - 1; i >= 0; i--) + { + second_hash = (second_hash + (t[i] * m)) % p; + m = (m * q) % p; + } + + for (int i = 0; i < s.Length - 1; i++) + { + xt = (xt * q) % p; + } + + for (int i = 1; i < t.Length - s.Length + 1; i++) + { + if (first_hash == second_hash) + { + return i - 1; + } + + second_hash = q * (second_hash - (t[i - 1] * xt)); + second_hash += t[i + 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 EnterValues() + { + string s = Console.ReadLine(); + string t = Console.ReadLine(); + + int result = Rabin_Karp(s, t); + + Console.WriteLine(result); + } + } +} diff --git a/CourseApp/Module3/CycStr.cs b/CourseApp/Module3/CycStr.cs new file mode 100644 index 0000000..76d378a --- /dev/null +++ b/CourseApp/Module3/CycStr.cs @@ -0,0 +1,45 @@ +using System; + +namespace CourseApp.Module3 +{ + public class CycStr + { + public static int[] Prefix_function(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 EnterValues() + { + string s = Console.ReadLine(); + + int[] prefixs = Prefix_function(s); + + int result = s.Length - prefixs[s.Length - 1]; + + Console.WriteLine(result); + } + } +} diff --git a/CourseApp/Module3/LinePer.cs b/CourseApp/Module3/LinePer.cs new file mode 100644 index 0000000..d4609b9 --- /dev/null +++ b/CourseApp/Module3/LinePer.cs @@ -0,0 +1,52 @@ +using System; + +namespace CourseApp.Module3 +{ + public class LinePer + { + public static int[] Prefix_function(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 EnterValues() + { + string s = Console.ReadLine(); + + int[] prefixs = Prefix_function(s); + + int result = s.Length - prefixs[s.Length - 1]; + + if (s.Length % result == 0) + { + Console.WriteLine(s.Length / result); + } + else + { + Console.WriteLine(1); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/Searchsubstring.cs b/CourseApp/Module3/Searchsubstring.cs new file mode 100644 index 0000000..230aba0 --- /dev/null +++ b/CourseApp/Module3/Searchsubstring.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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 EnterValues() + { + string s = Console.ReadLine(); + string t = Console.ReadLine(); + + int p = 787898; + int x = 26; + + Rabin_karp(s, t, p, x); + } + } +} diff --git a/CourseApp/Module4/Correct_bracket_sequence.cs b/CourseApp/Module4/Correct_bracket_sequence.cs new file mode 100644 index 0000000..0ac0c04 --- /dev/null +++ b/CourseApp/Module4/Correct_bracket_sequence.cs @@ -0,0 +1,68 @@ +using System; + +namespace CourseApp.Module4 +{ + public class Correct_bracket_sequence + { + public static void CBS_Method() + { + string parentheses = Console.ReadLine(); + int counter = 0; + + for (int i = 0; i < parentheses.Length; i++) + { + if (parentheses[i] == '(') + { + Stack.Push(parentheses[i]); + } + else if (Stack.Empty() == false && parentheses[i] == ')') + { + Stack.Pop(); + } + else + { + counter += 1; + } + } + + Console.WriteLine(counter + Stack.Size()); + } + + private class Stack + { + private static int[] buffer = new int[1000000]; + 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/MinOnSeg.cs b/CourseApp/Module4/MinOnSeg.cs new file mode 100644 index 0000000..71c2a92 --- /dev/null +++ b/CourseApp/Module4/MinOnSeg.cs @@ -0,0 +1,137 @@ +using System; + +public class MinOnSeg +{ + public static void MinOnSeg_Method() + { + string sFirst = Console.ReadLine(); + string[] sFirstValues = sFirst.Split(' '); + int[] arrFirst = new int[2]; + for (int i = 0; i < 2; i++) + { + arrFirst[i] = int.Parse(sFirstValues[i]); + } + + string sSecond = Console.ReadLine(); + string[] sSecondValues = sSecond.Split(' '); + int[] arrSecond = new int[arrFirst[0]]; + for (int i = 0; i < arrFirst[0]; i++) + { + arrSecond[i] = int.Parse(sSecondValues[i]); + } + + for (int i = 0; i < arrFirst[1]; i++) + { + while (Deque.Empty() == false && arrSecond[i] < arrSecond[Deque.Front()]) + { + Deque.Pop_Front(); + } + + Deque.Push_Front(i); + } + + for (int i = arrFirst[1]; i < arrFirst[0]; i++) + { + Console.WriteLine(arrSecond[Deque.Back()]); + + while (Deque.Empty() == false && Deque.Back() <= i - arrFirst[1]) + { + Deque.Pop_Back(); + } + + while (Deque.Empty() == false && arrSecond[i] < arrSecond[Deque.Front()]) + { + Deque.Pop_Front(); + if (Deque.Size() == 0) + { + Deque.Clear(); + } + } + + Deque.Push_Front(i); + } + + Console.WriteLine(arrSecond[Deque.Back()]); + } + + private class Deque + { + private static int[] buffer = new int[100000001]; + private static int front = 0; + private static int back = 1000000 - 1; + private static int size = 0; + + public static void Push_Back(int t) + { + back++; + if (back == 1000000) + { + back = 0; + } + + buffer[back] = t; + size++; + } + + public static void Push_Front(int t) + { + front--; + if (front < 0) + { + front = 1000000 - 1; + } + + buffer[front] = t; + size++; + } + + public static void Pop_Back() + { + back--; + if (back < 0) + { + back = 1000000 - 1; + } + + size--; + } + + public static void Pop_Front() + { + front++; + if (front == 1000000) + { + front = 0; + } + + size--; + } + + public static void Clear() + { + front = 0; + back = 1000000 - 1; + size = 0; + } + + public static int Front() + { + return buffer[front]; + } + + public static int Back() + { + return buffer[back]; + } + + public static int Size() + { + return size; + } + + public static bool Empty() + { + return size == 0; + } + } +} diff --git a/CourseApp/Module4/NearSmaller.cs b/CourseApp/Module4/NearSmaller.cs new file mode 100644 index 0000000..cb780ad --- /dev/null +++ b/CourseApp/Module4/NearSmaller.cs @@ -0,0 +1,81 @@ +using System; + +public class NearSmaller +{ + public static void NearSmaller_Method() + { + var numberElements = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var sValues = s.Split(' '); + var arrayElements = new int[numberElements]; + var answerArray = new int[numberElements]; + for (int i = 0; i < numberElements; i++) + { + arrayElements[i] = int.Parse(sValues[i]); + } + + int index = numberElements - 1; + while (index >= 0) + { + while (Stack.Empty() == false && arrayElements[Stack.Back()] >= arrayElements[index]) + { + Stack.Pop(); + } + + if (Stack.Empty() == true) + { + answerArray[index] = -1; + } + else + { + answerArray[index] = Stack.Back(); + } + + Stack.Push(index); + + index--; + } + + for (int i = 0; i < numberElements; i++) + { + Console.Write(answerArray[i] + " "); + } + } + + private class Stack + { + private static int[] buffer = new int[1000000]; + 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/Sort.cs b/CourseApp/Module4/Sort.cs new file mode 100644 index 0000000..a7f533f --- /dev/null +++ b/CourseApp/Module4/Sort.cs @@ -0,0 +1,94 @@ +using System; + +namespace CourseApp.Module4 +{ + public class Sort + { + public static void Sort_Method() + { + var numberWagons = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var sValues = s.Split(' '); + var arrayWagons = new int[numberWagons]; + var answerArray = new int[numberWagons]; + for (int i = 0; i < numberWagons; i++) + { + arrayWagons[i] = int.Parse(sValues[i]); + } + + int a = 0; + int b = 0; + + while (a != numberWagons) + { + if (Stack.Empty() == true || (b < numberWagons && arrayWagons[b] < Stack.Back())) + { + Stack.Push(arrayWagons[b]); + b++; + } + else + { + answerArray[a] += Stack.Back(); + Stack.Pop(); + a++; + } + } + + bool isAnswer = true; + + for (int i = 0; i < answerArray.Length - 1; i++) + { + if (answerArray[i] > answerArray[i + 1]) + { + isAnswer = false; + } + } + + if (isAnswer == true) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + private class Stack + { + private static int[] buffer = new int[1000000]; + 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/Balanced_Tree.cs b/CourseApp/Module5/Balanced_Tree.cs new file mode 100644 index 0000000..31d315f --- /dev/null +++ b/CourseApp/Module5/Balanced_Tree.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module5 +{ + public class Balanced_Tree + { + private Node root; + + public static void BalTreeMeth() + { + string s = Console.ReadLine(); + + string[] sValues = s.Split(' '); + + var tree = new Balanced_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 bool IsBalanced(Node node) + { + int left_hight; + + int right_hight; + + if (node == null) + { + return true; + } + + left_hight = H(node.Left); + right_hight = H(node.Right); + + if (Math.Abs(left_hight - right_hight) <= 1 && IsBalanced(node.Left) + && IsBalanced(node.Right)) + { + return true; + } + + return false; + } + + public virtual int H(Node node) + { + if (node == null) + { + return 0; + } + + return 1 + Math.Max(H(node.Left), H(node.Right)); + } + + public void Insert(int a) + { + root = InnerInsert(a, 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 a, Node root) + { + if (root == null) + { + return new Node(a); + } + + if (root.Data > a) + { + root.Left = InnerInsert(a, root.Left); + } + else if (root.Data < a) + { + root.Right = InnerInsert(a, root.Right); + } + + return root; + } + + public class Node + { + public Node(int a) + { + Data = a; + Left = null; + Right = null; + } + + public Node Left { get; set; } + + public Node Right { get; set; } + + public int Data { get; set; } + } + } +} diff --git a/CourseApp/Module5/Bin_Tree.cs b/CourseApp/Module5/Bin_Tree.cs new file mode 100644 index 0000000..07fdba9 --- /dev/null +++ b/CourseApp/Module5/Bin_Tree.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module5 +{ + public class Bin_Tree + { + private Node root; + private List size; + + public static void BinTreeMeth() + { + string s = Console.ReadLine(); + + string[] sValues = s.Split(' '); + + var tree = new Bin_Tree(); + + for (int i = 0; i < sValues.Length - 1; i++) + { + tree.Insert(int.Parse(sValues[i])); + } + + tree.FindLastElem(); + } + + public void Insert(int a) + { + root = InnerInsert(a, root); + } + + public List GetValues() + { + size = new List(); + InnerTraversal(root); + return size; + } + + private bool InnerFind(int a, Node root) + { + if (root == null) + { + return false; + } + + if (a == root.Data) + { + return true; + } + else if (a < root.Data) + { + return InnerFind(a, root.L); + } + else + { + return InnerFind(a, root.R); + } + } + + private bool Find(int a) + { + return InnerFind(a, root); + } + + private void FindLastElem() + { + LastElem(root); + } + + private void LastElem(Node value) + { + if (value == null) + { + return; + } + + LastElem(value.L); + + if ((value.L != null && value.R == null) || (value.R != null && value.L == null)) + { + Console.WriteLine(value.Data); + } + + LastElem(value.R); + } + + private Node InnerInsert(int a, Node root) + { + if (root == null) + { + return new Node(a); + } + + if (root.Data > a) + { + root.L = InnerInsert(a, root.L); + } + else if (root.Data < a) + { + root.R = InnerInsert(a, root.R); + } + + return root; + } + + private void InnerTraversal(Node node) + { + if (node == null) + { + return; + } + + InnerTraversal(node.L); + size.Add(node.Data); + InnerTraversal(node.R); + } + + internal class Node + { + public Node(int a) + { + Data = a; + L = null; + R = null; + } + + public Node L { get; set; } + + public Node R { 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..5c79bc7 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,23 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - - Console.WriteLine("Hello World"); + // BubbleSort.BubbleSortM(); // + // BubbleSortP.BubbleSortM(); // + // Sort_Merge.Try(); // + // Invers_Sort.Try(); // + // Count_Dif.Try(); // + // Storage_Sort.Try(); // + // Searchsubstring.EnterValues(); // + // CycShift.EnterValues(); // + // LinePer.EnterValues(); // + // CycStr.EnterValues(); // + // Correct_bracket_sequence.CBS_Method(); // + // Sort.Sort_Method(); // + // NearSmaller.NearSmaller_Method(); // + // MinOnSeg.MinOnSeg_Method(); // + // Bin_Tree.BinTreeMeth(); // + // Balanced_Tree.BalTreeMeth(); // + BitwiseSort.Method(); } } } diff --git a/README.md b/README.md index e58af8a..0c8e100 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Tprogramming_42_2020 -Master branch :) \ No newline at end of file +Batasov Sergey (T_T) \ No newline at end of file