diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index ae46394..352266c 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + netcoreapp5.0 True 1573,1591,1701;1702;1705 false diff --git a/CourseApp.Tests/Module1_2/WwriteTest.cs b/CourseApp.Tests/Module1_2/WwriteTest.cs new file mode 100644 index 0000000..3ce5223 --- /dev/null +++ b/CourseApp.Tests/Module1_2/WwriteTest.cs @@ -0,0 +1,41 @@ +using System; +using System.IO; +using Xunit; +using CourseApp.Module1_2; + +namespace CourseApp.Tests.Module1_2 +{ + [Collection("Sequential")] + public class WwriteTest : IDisposable + { + 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("Vasya", "Hello, Vasya!")] + [InlineData("Petya", "Hello, Petya!")] + [InlineData("Olya", "Hello, Olya!")] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + Wwrite.WriteHello(input); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + Assert.Equal($"{expected}", output[0]); + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + } + } +} diff --git a/CourseApp.Tests/Module2/BubbleSortTest.cs b/CourseApp.Tests/Module2/BubbleSortTest.cs index 8a9b192..80f9e09 100644 --- a/CourseApp.Tests/Module2/BubbleSortTest.cs +++ b/CourseApp.Tests/Module2/BubbleSortTest.cs @@ -1,18 +1,27 @@ -using System; -using System.IO; -using Xunit; -using CourseApp.Module2; - -namespace CourseApp.Tests.Module2 +namespace CourseApp.Tests.Module2 { + using System; + 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 +33,9 @@ public void Dispose() } [Theory] - [InlineData(Inp1, "1 1 3 4 5 7 9")] - [InlineData(Inp2, "-10 2 7")] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) { var stringWriter = new StringWriter(); @@ -39,7 +49,9 @@ public void Test1(string input, string expected) // assert var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - Assert.Equal($"{expected}", output[0]); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); } } -} +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/InversionSTest.cs b/CourseApp.Tests/Module2/InversionSTest.cs new file mode 100644 index 0000000..a3179b7 --- /dev/null +++ b/CourseApp.Tests/Module2/InversionSTest.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class InversionSTest : 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 + InversionS.MergeMain(); + + // 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/MergeSorttTest.cs b/CourseApp.Tests/Module2/MergeSorttTest.cs new file mode 100644 index 0000000..5f5b38a --- /dev/null +++ b/CourseApp.Tests/Module2/MergeSorttTest.cs @@ -0,0 +1,71 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class MergeSorttTest : 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 = @"2 +3 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(new int[] { 1 }, new int[] { 2 }, new int[] { 1, 2 })] + public void TestMerge(int[] a, int[] b, int[] exp) + { + var res = MergeSort.MergeSortMethod(a, b); + Assert.Equal(exp, res); + }*/ + + [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.MergeMain(); + + // 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/Number_of_different_Test.cs b/CourseApp.Tests/Module2/Number_of_different_Test.cs new file mode 100644 index 0000000..82d06e9 --- /dev/null +++ b/CourseApp.Tests/Module2/Number_of_different_Test.cs @@ -0,0 +1,46 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class Number_of_different_Test : 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 + Number_of_different.ClassMainNumb(); + + // 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/Task6Sort_Test.cs b/CourseApp.Tests/Module2/Task6Sort_Test.cs new file mode 100644 index 0000000..269cdea --- /dev/null +++ b/CourseApp.Tests/Module2/Task6Sort_Test.cs @@ -0,0 +1,52 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class Task6Sort_Test : 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 + Task6Sort.Task6_Main(); + + // 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/pairsTest.cs b/CourseApp.Tests/Module2/pairsTest.cs new file mode 100644 index 0000000..3c6dceb --- /dev/null +++ b/CourseApp.Tests/Module2/pairsTest.cs @@ -0,0 +1,60 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class PairsTest : 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 + PairsSort.PairsS(); + + // 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/Cyclic_shift_Test.cs b/CourseApp.Tests/Module3/Cyclic_shift_Test.cs new file mode 100644 index 0000000..833257c --- /dev/null +++ b/CourseApp.Tests/Module3/Cyclic_shift_Test.cs @@ -0,0 +1,51 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class Cyclic_shift_Test : IDisposable + { + private const string Inp3 = @"zabcd +abcdz"; + + private const string Out3 = @"4"; + + private const string Inp1 = @"a +b"; + + 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(Inp3, Out3)] + [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 + Cyclic_shift.Cyclic_shift_Main(); + + // 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/Cyclic_string_Test.cs b/CourseApp.Tests/Module3/Cyclic_string_Test.cs new file mode 100644 index 0000000..511c308 --- /dev/null +++ b/CourseApp.Tests/Module3/Cyclic_string_Test.cs @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class Cyclic_string_Test : IDisposable + { + private const string Inp3 = @"Z"; + + private const string Out3 = @"1"; + + private const string Inp2 = @"ABCDABC"; + + 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(Inp3, Out3)] + [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 + Cyclic_string.Cyclic_Main(); + + // 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/Line_Test.cs b/CourseApp.Tests/Module3/Line_Test.cs new file mode 100644 index 0000000..ab8f3f5 --- /dev/null +++ b/CourseApp.Tests/Module3/Line_Test.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class Line_Test : IDisposable + { + private const string Inp3 = @"ababbababa +aba"; + + private const string Out3 = @"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(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 + Line.Line_Main(); + + // 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/RabinKarpTest.cs b/CourseApp.Tests/Module3/RabinKarpTest.cs new file mode 100644 index 0000000..5497d5d --- /dev/null +++ b/CourseApp.Tests/Module3/RabinKarpTest.cs @@ -0,0 +1,52 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class RabinKarpTest : IDisposable + { + private const string Inp3 = @"aaaaa"; + + private const string Out3 = @"5"; + + private const string Inp2 = @"abcabcabc"; + + private const string Out2 = @"3"; + + private const string Inp1 = @"abab"; + + private const string Out1 = @"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(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 + Rabin_karp.Rabin_karp_Method(); + + // 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/Nearest_smaller_Test.cs b/CourseApp.Tests/Module4/Nearest_smaller_Test.cs new file mode 100644 index 0000000..be00e8e --- /dev/null +++ b/CourseApp.Tests/Module4/Nearest_smaller_Test.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class Nearest_smaller_Test : IDisposable + { + private const string Inp3 = @"10 +1 2 3 2 1 4 2 5 3 1"; + + private const string Out3 = @"-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(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 + Nearest_smallerA.Nearest_smaller_Main(); + + // 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/PSP_TEST.cs b/CourseApp.Tests/Module4/PSP_TEST.cs new file mode 100644 index 0000000..e058907 --- /dev/null +++ b/CourseApp.Tests/Module4/PSP_TEST.cs @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class PSP_TEST : IDisposable + { + private const string Inp3 = @"())(()"; + + private const string Out3 = @"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(Inp3, Out3)] + [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.Main_PSP(); + + // 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/TrainStack_TEST.cs b/CourseApp.Tests/Module4/TrainStack_TEST.cs new file mode 100644 index 0000000..f759757 --- /dev/null +++ b/CourseApp.Tests/Module4/TrainStack_TEST.cs @@ -0,0 +1,51 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class TrainStack_TEST : IDisposable + { + private const string Inp3 = @"3 +3 2 1"; + + private const string Out3 = @"YES"; + + private const string Inp2 = @"4 +4 1 3 2"; + + private const string Out2 = @"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(Inp3, Out3)] + [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 + TrainStack.Main_train(); + + // 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/WindowTest.cs b/CourseApp.Tests/Module4/WindowTest.cs new file mode 100644 index 0000000..1a35a8e --- /dev/null +++ b/CourseApp.Tests/Module4/WindowTest.cs @@ -0,0 +1,50 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class WindowTest : IDisposable + { + private const string Inp3 = @"7 3 +1 3 2 4 5 3 1 +"; + + private const string Out3 = @"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(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 + WindowA.Window_Main(); + + // 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/Module5/BalancedTree_Test.cs b/CourseApp.Tests/Module5/BalancedTree_Test.cs new file mode 100644 index 0000000..fedc8b0 --- /dev/null +++ b/CourseApp.Tests/Module5/BalancedTree_Test.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.IO; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class BalancedTree_Test : IDisposable + { + private const string Inp3 = @"7 3 2 1 9 5 4 6 8 0"; + + private const string Out3 = @"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(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 + BalancedTree.TreeB(); + + // 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/Module5/Less1_Test.cs b/CourseApp.Tests/Module5/Less1_Test.cs new file mode 100644 index 0000000..25ab5a9 --- /dev/null +++ b/CourseApp.Tests/Module5/Less1_Test.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.IO; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class Less1_Test : IDisposable + { + private const string Inp3 = @"7 3 2 1 9 5 4 6 8 0"; + + private const string Out3 = @"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(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 + Less1.TreeW(); + + // 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.sln b/CourseApp.sln index 4c8426c..50d7460 100644 --- a/CourseApp.sln +++ b/CourseApp.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.0.32014.148 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31702.278 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CourseApp", "CourseApp\CourseApp.csproj", "{D1856C82-5A61-4A61-A2CE-BDAF53530918}" EndProject diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index 9551450..76f3b2f 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp2.1 + netcoreapp5.0 True 1573,1591,1701;1702;1705; diff --git a/CourseApp/Module1_2/Wwrite.cs b/CourseApp/Module1_2/Wwrite.cs new file mode 100644 index 0000000..0629618 --- /dev/null +++ b/CourseApp/Module1_2/Wwrite.cs @@ -0,0 +1,14 @@ +using System; + +namespace CourseApp.Module1_2 +{ + public class Wwrite + { + public static void WriteHello(string name) + { + string finish; + finish = "Hello, " + name + "!"; + Console.WriteLine(finish); + } + } +} diff --git a/CourseApp/Module2/BitwiseSort.cs b/CourseApp/Module2/BitwiseSort.cs new file mode 100644 index 0000000..14b9c80 --- /dev/null +++ b/CourseApp/Module2/BitwiseSort.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace CourseApp.Module2 +{ + public class BitwiseSort + { + public static void Radixsort(string[] arrayStrings) + { + Console.WriteLine("Initial array:"); + Console.WriteLine(string.Join(", ", arrayStrings)); + int poz = 1; + int rank = arrayStrings[0].Length; + + 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 {poz}"); + arrayStrings = CountSort(arrayStrings, poz, rank); + poz++; + } + + Console.WriteLine("**********"); + Console.WriteLine("Sorted array:"); + Console.Write(string.Join(", ", arrayStrings)); + } + + public static string[] CountSort(string[] arrayString, int phase, int lenght) + { + int i; + List[] arrayListens = new List[10]; + for (int x = 0; x < 10; x++) + { + arrayListens[x] = new List(); + } + + for (int j = 0; j < arrayString.Length; j++) + { + int y = int.Parse(arrayString[j].Substring(lenght - phase, 1)); + arrayListens[y].Add(arrayString[j]); + } + + for (i = 0; i < 10; i++) + { + if (arrayListens[i].Count == 0) + { + Console.WriteLine("Bucket " + i + ": empty"); + } + else + { + Console.WriteLine("Bucket " + i + ": {0}", string.Join(", ", arrayListens[i])); + } + } + + int z = 0; + + for (i = 0; i < 10; i++) + { + for (int j = 0; j < arrayListens[i].Count; j++) + { + arrayString[z] = arrayListens[i][j]; + z++; + } + } + + return arrayString; + } + + public static void BitwiseSortMain() + { + int n = Convert.ToInt32(Console.ReadLine()); + string[] arr_str = new string[n]; + for (int i = 0; i < n; i++) + { + arr_str[i] = Console.ReadLine(); + } + + Radixsort(arr_str); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..42230ac 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace CourseApp.Module2 { @@ -17,22 +15,25 @@ public static void BubbleSortMethod() arr[i] = int.Parse(sValues[i]); } + bool protocol0 = false; for (int i = 0; i < arr.Length - 1; i++) { for (int j = 0; j < arr.Length - i - 1; j++) { if (arr[j] > arr[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]); + string result = string.Join(" ", arr); + Console.WriteLine(result); + protocol0 = true; } } } - string result = string.Join(" ", arr); - Console.WriteLine(result); + if (protocol0 == false) + { + Console.WriteLine(0); + } } } -} +} \ No newline at end of file diff --git a/CourseApp/Module2/InversionS.cs b/CourseApp/Module2/InversionS.cs new file mode 100644 index 0000000..87fa94b --- /dev/null +++ b/CourseApp/Module2/InversionS.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 InversionS + { + private static long invers = 0; + + public static int[] MergInversion(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++; + invers += a.Length - i; + } + } + + return y; + } + + public static int[] MergeIn(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 = MergeIn(v, l, m); + int[] right = MergeIn(v, m, r); + + int[] sort = MergInversion(left, right); + return sort; + } + + public static void MergeMain() + { + 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 = MergeIn(arr, 0, n); + + Console.WriteLine(invers); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/MergeSort.cs b/CourseApp/Module2/MergeSort.cs new file mode 100644 index 0000000..20239a3 --- /dev/null +++ b/CourseApp/Module2/MergeSort.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 MergeSort + { + public static int[] MergSortMethod(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[] MergeSorting(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 = MergeSorting(v, l, m); + int[] right = MergeSorting(v, m, r); + + int[] sort = MergSortMethod(left, right); + + Console.WriteLine("{0} {1} {2} {3}", l + 1, r, sort[0], sort[^1]); + + return MergSortMethod(left, right); + } + + public static void MergeMain() + { + 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 = MergeSorting(arr, 0, n); + + Console.WriteLine(string.Join(" ", v_sorted)); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/Number_of_different.cs b/CourseApp/Module2/Number_of_different.cs new file mode 100644 index 0000000..c6ded91 --- /dev/null +++ b/CourseApp/Module2/Number_of_different.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class Number_of_different + { + private static long inversion = 0; + + public static int[] MergSort(int[] a, int[] b) + { + int x = 0; + int y = 0; + int[] arr = new int[a.Length + b.Length]; + for (int i = 0; i < arr.Length; i++) + { + if (x == a.Length) + { + arr[i] = b[y]; + y++; + } + else if (y == b.Length) + { + arr[i] = a[x]; + x++; + } + else if (a[x] <= b[y]) + { + arr[i] = a[x]; + x++; + } + else + { + arr[i] = b[y]; + y++; + inversion += a.Length - x; + } + } + + return arr; + } + + public static int[] MergeSort1(int[] x, int l, int r) + { + if (r - l == 1) + { + int[] result = new int[1]; + result[0] = x[l]; + return result; + } + + int m = (l + r) / 2; + + int[] left = MergeSort1(x, l, m); + int[] right = MergeSort1(x, m, r); + + int[] sort = MergSort(left, right); + + return sort; + } + + public static void ClassMainNumb() + { + int n = Convert.ToInt32(Console.ReadLine()); + string str = Console.ReadLine(); + string[] str_value = str.Split(' '); + int[] arr = new int[n]; + for (int j = 0; j < n; j++) + { + arr[j] = Convert.ToInt32(str_value[j]); + } + + int[] arr_sorted = MergeSort1(arr, 0, n); + + int count = 1; + for (int i = 1; i < arr_sorted.Length; i++) + { + if (arr_sorted[i - 1] != arr_sorted[i]) + { + count++; + } + } + + Console.WriteLine(count); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/PairsSort.cs b/CourseApp/Module2/PairsSort.cs new file mode 100644 index 0000000..7c48d8f --- /dev/null +++ b/CourseApp/Module2/PairsSort.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class PairsSort + { + public static void PairsS() + { + int sortPairs = Convert.ToInt32(Console.ReadLine()); + int[,] pairs = new int[sortPairs, 2]; + for (int i = 0; i < sortPairs; i++) + { + string value = Console.ReadLine(); + string[] sValues = value.Split(' '); + pairs[i, 0] = int.Parse(sValues[0]); + pairs[i, 1] = int.Parse(sValues[1]); + } + + for (int i = 0; i < (pairs.Length / 2) - 1; i++) + { + for (int q = 0; q < (pairs.Length / 2) - i - 1; q++) + { + if (pairs[q, 1] < pairs[q + 1, 1]) + { + (pairs[q, 1], pairs[q + 1, 1]) = (pairs[q + 1, 1], pairs[q, 1]); + (pairs[q, 0], pairs[q + 1, 0]) = (pairs[q + 1, 0], pairs[q, 0]); + } + + if (pairs[q, 1] == pairs[q + 1, 1]) + { + if (pairs[q, 0] > pairs[q + 1, 0]) + { + (pairs[q, 0], pairs[q + 1, 0]) = (pairs[q + 1, 0], pairs[q, 0]); + (pairs[q, 1], pairs[q + 1, 1]) = (pairs[q + 1, 1], pairs[q, 1]); + } + } + } + } + + for (int i = 0; i < sortPairs; i++) + { + Console.WriteLine($"{pairs[i, 0]} {pairs[i, 1]}"); + } + } + } +} diff --git a/CourseApp/Module2/Task6Sort.cs b/CourseApp/Module2/Task6Sort.cs new file mode 100644 index 0000000..c0c651a --- /dev/null +++ b/CourseApp/Module2/Task6Sort.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class Task6Sort + { + public static void Sravn(int[] arr_data, int[] order_data) + { + for (int i = 0; i < arr_data.Length; i++) + { + if (arr_data[i] >= order_data[i]) + { + Console.WriteLine("no"); + } + else + { + Console.WriteLine("yes"); + } + } + } + + public static int[] Raschet(int[] arr_data, int siz) + { + int size = siz; + int[] order_data = new int[size]; + int count = 0; + bool flag = false; + int k = 0; + for (int i = 0; i < arr_data.Length; i++) + { + if ((i == arr_data.Length - 1) && (arr_data[i] != arr_data[i - 1])) + { + order_data[k] = 1; + } + + if (i == arr_data.Length - 1) + { + break; + } + else if (arr_data[i] == arr_data[i + 1]) + { + count++; + if (i == arr_data.Length - 2) + { + flag = true; + } + } + else if (arr_data[i] != arr_data[i + 1]) + { + flag = true; + } + + if (flag) + { + order_data[k] = ++count; + k++; + count = 0; + flag = false; + } + } + + return order_data; + } + + public static int[] MergSortMethod(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[] MergeSorting(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 = MergeSorting(v, l, m); + int[] right = MergeSorting(v, m, r); + + int[] sort = MergSortMethod(left, right); + + return MergSortMethod(left, right); + } + + public static void Task6_Main() + { + int size_sklad = Convert.ToInt32(Console.ReadLine()); + string str_sklad = Console.ReadLine(); + string[] string_sklad = str_sklad.Split(' '); + int[] data = new int[size_sklad]; + for (int i = 0; i < size_sklad; i++) + { + data[i] = Convert.ToInt32(string_sklad[i]); + } + + int size_zakaz = Convert.ToInt32(Console.ReadLine()); + string str_zakaz = Console.ReadLine(); + string[] string_zakaz = str_zakaz.Split(' '); + int[] data_zakaz = new int[size_zakaz]; + for (int i = 0; i < size_zakaz; i++) + { + data_zakaz[i] = Convert.ToInt32(string_zakaz[i]); + } + + int[] zakaz_sorted = MergeSorting(data_zakaz, 0, size_zakaz); + int[] res = Raschet(zakaz_sorted, size_sklad); + Sravn(data, res); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/Cyclic_shift.cs b/CourseApp/Module3/Cyclic_shift.cs new file mode 100644 index 0000000..9ad7454 --- /dev/null +++ b/CourseApp/Module3/Cyclic_shift.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module3 +{ + public class Cyclic_shift + { + public static int Cyclic_shift_Method(string s, string t, long p, long x) + { + long m = 1; + long xt = 1; + long firstHash = 0; + long secondHash = 0; + + if (t == s) + { + return 0; + } + + t = string.Concat(Enumerable.Repeat(t, 2)); + + foreach (char i in s.Reverse()) + { + firstHash = (firstHash + (i * m)) % p; + m = (m * x) % p; + } + + m = 1; + + for (int i = s.Length - 1; i >= 0; i--) + { + secondHash = (secondHash + (t[i] * m)) % p; + m = (m * x) % p; + } + + for (int i = 0; i < s.Length - 1; i++) + { + xt = (xt * x) % p; + } + + for (int i = 1; i < t.Length - s.Length + 1; i++) + { + if (firstHash == secondHash) + { + return i - 1; + } + + secondHash = ((x * (secondHash - (t[i - 1] * xt))) + t[i + s.Length - 1]) % p; + + if ((secondHash < 0 && p > 0) || (secondHash > 0 && p < 0)) + { + secondHash += p; + } + } + + return -1; + } + + public static void Cyclic_shift_Main() + { + string ht = Console.ReadLine(); + string hs = Console.ReadLine(); + + long p = 1000000000; + long x = 13; + + int res = Cyclic_shift_Method(ht, hs, p, x); + Console.WriteLine(res); + } + } +} diff --git a/CourseApp/Module3/Cyclic_string.cs b/CourseApp/Module3/Cyclic_string.cs new file mode 100644 index 0000000..9ad6c6f --- /dev/null +++ b/CourseApp/Module3/Cyclic_string.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module3 +{ + public class Cyclic_string + { + public static int Cyclic_String_Method(string s) + { + int[] z = new int[s.Length]; + z[0] = 0; + + for (int i = 0; i < s.Length - 1; i++) + { + int mid = z[i]; + + while (mid > 0 && s[i + 1] != s[mid]) + { + mid = z[mid - 1]; + } + + if (s[i + 1] == s[mid]) + { + z[i + 1] = mid + 1; + } + else + { + z[i + 1] = 0; + } + } + + int res = s.Length - z[s.Length - 1]; + return res; + } + + public static void Cyclic_Main() + { + string s = Console.ReadLine(); + + int result = Cyclic_String_Method(s); + Console.WriteLine(result); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/Line.cs b/CourseApp/Module3/Line.cs new file mode 100644 index 0000000..5df3ad5 --- /dev/null +++ b/CourseApp/Module3/Line.cs @@ -0,0 +1,26 @@ +using System; + +namespace CourseApp.Module3 +{ + public class Line + { + public static void Line_Main() + { + int i = 0; + int x = -1; + string s = Console.ReadLine(); + string t = Console.ReadLine(); + + while (i != -1) + { + i = s.IndexOf(t, x + 1); + if (i != -1) + { + Console.Write(i + " "); + } + + x = i; + } + } + } +} diff --git a/CourseApp/Module3/Line_2.cs b/CourseApp/Module3/Line_2.cs new file mode 100644 index 0000000..d35c490 --- /dev/null +++ b/CourseApp/Module3/Line_2.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module3 +{ + using System; + + public static class Line_2 + { + public static void RabinCarpMethod() + { + const int k = 31; + const int mod = 1000000007; + + string original = Console.ReadLine(); + string pattern = Console.ReadLine(); + + long originalHash = HashFunction(original, pattern.Length, k, mod); + long patternHash = HashFunction(pattern, pattern.Length, k, mod); + + long m = 1; + for (int i = 0; i < pattern.Length; i++) + { + m = (m * k) % mod; + } + + for (int i = 0; i <= original.Length - pattern.Length; i++) + { + if (patternHash == originalHash) + { + Console.Write(i + " "); + } + + if (i + pattern.Length < original.Length) + { + originalHash = ((originalHash * k) - (original[i] * m) + original[i + pattern.Length]) % mod; + originalHash = (originalHash + mod) % mod; + } + } + } + + private static long HashFunction(string inp, int stringLength, int x, int p) + { + long h = 0; + for (int i = 0; i < stringLength; i++) + { + h = ((h * x) + inp[i]) % p; + } + + return h; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/Rabin_karp.cs b/CourseApp/Module3/Rabin_karp.cs new file mode 100644 index 0000000..85a3356 --- /dev/null +++ b/CourseApp/Module3/Rabin_karp.cs @@ -0,0 +1,43 @@ +using System; + +namespace CourseApp.Module3 +{ + public class Rabin_karp + { + public static void Rabin_karp_Method() + { + string s = Console.ReadLine(); + 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; + } + } + + int finish = s.Length - res[s.Length - 1]; + + if (s.Length % finish == 0) + { + Console.WriteLine(s.Length / finish); + } + else + { + Console.WriteLine(1); + } + } + } +} diff --git a/CourseApp/Module4/Class1.cs b/CourseApp/Module4/Class1.cs new file mode 100644 index 0000000..c9d966a --- /dev/null +++ b/CourseApp/Module4/Class1.cs @@ -0,0 +1,142 @@ +using System; + +public class Class1 +{ + public static void WindowB(int[] collArr, int[] windowArray) + { + for (int g = 0; g < collArr[1]; g++) + { + while (Deque.Empty() == false && windowArray[g] < windowArray[Deque.Front()]) + { + Deque.Pop_Front(); + } + + Deque.Push_Front(g); + } + + for (int g = collArr[1]; g < collArr[0]; g++) + { + Console.WriteLine(windowArray[Deque.Back()]); + + while (Deque.Empty() == false && Deque.Back() <= g - collArr[1]) + { + Deque.Pop_Back(); + } + + while (Deque.Empty() == false && windowArray[g] < windowArray[Deque.Front()]) + { + Deque.Pop_Front(); + if (Deque.Size() == 0) + { + Deque.Clear(); + } + } + + Deque.Push_Front(g); + } + + Console.WriteLine(windowArray[Deque.Back()]); + } + +/* public static void RTMain() + { + 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]); + } + + WindowB(array1, array2); + }*/ + + 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/DequexItemA.cs b/CourseApp/Module4/DequexItemA.cs new file mode 100644 index 0000000..69df241 --- /dev/null +++ b/CourseApp/Module4/DequexItemA.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class DequexItemA + { + public T Data { get; set; } + + public DequexItemA Next { get; set; } + + public DequexItemA Previous { get; set; } + +/* public DequexItem(T data) + { + Data = data; + }*/ + + public override string ToString() + { + return Data.ToString(); + } + } +} diff --git a/CourseApp/Module4/EasyDequeA.cs b/CourseApp/Module4/EasyDequeA.cs new file mode 100644 index 0000000..7ad41b4 --- /dev/null +++ b/CourseApp/Module4/EasyDequeA.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class EasyDequeA + { + private List items = new List(); + + private T Head => items.Last(); + + private T Tail => items.First(); + + public int Size() + { + return items.Count; + } + + public void PushBack(T data) + { + items.Insert(0, data); + } + + public void PushFront(T data) + { + items.Add(data); + } + + public T POPback() + { + var item = Tail; + items.Remove(item); + return item; + } + + public T POPfront() + { + var item = Head; + items.Remove(item); + return item; + } + + public T PeekBack() + { + return Tail; + } + + public T PeekFront() + { + return Head; + } + + public T Min() + { + return items.Min(); + } + } +} diff --git a/CourseApp/Module4/EasyStack.cs b/CourseApp/Module4/EasyStack.cs new file mode 100644 index 0000000..c199589 --- /dev/null +++ b/CourseApp/Module4/EasyStack.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class EasyStack + { + private List items = new List(); + + private int sizze = 0; + + public int Count => items.Count; + + public bool Isempty => items.Count == 0; + + public int Size() + { + return sizze; + } + + public void Push(T item) + { + items.Add(item); + sizze++; + } + + public T POP() + { + if (!Isempty) + { + var item = items.LastOrDefault(); + var itemsIndex = items.LastIndexOf(item); + items.RemoveAt(itemsIndex); + sizze--; + return item; + } + else + { + throw new NullReferenceException("stack off"); + } + } + + public T Peek() + { + if (!Isempty) + { + return items.LastOrDefault(); + } + else + { + throw new NullReferenceException("stack off"); + } + } + } +} diff --git a/CourseApp/Module4/Nearest_smallerA.cs b/CourseApp/Module4/Nearest_smallerA.cs new file mode 100644 index 0000000..4be9be3 --- /dev/null +++ b/CourseApp/Module4/Nearest_smallerA.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class Nearest_smallerA + { + public static void Nearest_smaller_Method(int coll, string[] data) + { + var easyStack = new EasyStack(); + int[] arr = new int[coll]; + int[] arrAnswer = new int[coll]; + + for (int i = 0; i < coll; i++) + { + arr[i] = int.Parse(data[i]); + } + + for (int i = coll - 1; i >= 0; i--) + { + while (easyStack.Size() > 0 && arr[easyStack.Peek()] >= arr[i]) + { + easyStack.POP(); + } + + if (easyStack.Size() == 0) + { + arrAnswer[i] = -1; + } + else + { + arrAnswer[i] = easyStack.Peek(); + } + + easyStack.Push(i); + } + + for (int i = 0; i < coll; i++) + { + Console.Write($"{arrAnswer[i]} "); + } + } + + public static void Nearest_smaller_Main() + { + int q = int.Parse(Console.ReadLine()); + string[] data = Console.ReadLine().Split(' '); + + Nearest_smaller_Method(q, data); + } + } +} diff --git a/CourseApp/Module4/PSP.cs b/CourseApp/Module4/PSP.cs new file mode 100644 index 0000000..b70f886 --- /dev/null +++ b/CourseApp/Module4/PSP.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class PSP + { + public static void PSPmet(string q) + { + var easyStack = new EasyStack(); + + for (int i = 0; i < q.Length; i++) + { + easyStack.Push(q[i]); + } + + int open = 0; + int clous = 0; + + while (easyStack.Size() > 0) + { + if (clous == 0 && easyStack.Peek() == '(') + { + open++; + easyStack.POP(); + } + else if (clous > 0 && easyStack.Peek() == '(') + { + clous--; + easyStack.POP(); + } + else if (easyStack.Peek() == ')') + { + clous++; + easyStack.POP(); + } + else if (clous <= 0 && easyStack.Peek() == ')') + { + clous++; + easyStack.POP(); + } + } + + Console.WriteLine(open + clous); + } + + public static void Main_PSP() + { + string q = Console.ReadLine(); + PSPmet(q); + } + } +} diff --git a/CourseApp/Module4/TrainStack.cs b/CourseApp/Module4/TrainStack.cs new file mode 100644 index 0000000..fd0e2cd --- /dev/null +++ b/CourseApp/Module4/TrainStack.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class TrainStack + { + public static void Train(int lens, string[] numb) + { + var easyStack = new EasyStack(); + + int count = 1; + bool verefications = true; + + for (int i = 0; i < lens; i++) + { + int inp = int.Parse(numb[i]); + + if (easyStack.Size() > 0 && inp > easyStack.Peek()) + { + verefications = false; + break; + } + + easyStack.Push(inp); + while (easyStack.Size() > 0 && count == easyStack.Peek()) + { + easyStack.POP(); + count++; + } + } + + if (verefications) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + public static void Main_train() + { + int lens = int.Parse(Console.ReadLine()); + string[] numb = Console.ReadLine().Split(' '); + + Train(lens, numb); + } + } +} diff --git a/CourseApp/Module4/WindowA.cs b/CourseApp/Module4/WindowA.cs new file mode 100644 index 0000000..f7e7dc6 --- /dev/null +++ b/CourseApp/Module4/WindowA.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class WindowA + { + public static void Window_method(int[] collArr, string[] windowA) + { + var easyDeque = new EasyDequeA(); + + int sizearray = collArr[0]; + + int sizewindow = collArr[1]; + + int[] windowArr = new int[sizearray]; + + for (int i = 0; i < sizearray; i++) + { + windowArr[i] = int.Parse(windowA[i]); + } + + for (int i = 0; i < sizearray;) + { + while (easyDeque.Size() < sizewindow) + { + easyDeque.PushFront(windowArr[i]); + + i++; + } + + Console.WriteLine(easyDeque.Min()); + easyDeque.POPback(); + } + } + + public static void Window_Main() + { + string[] coll = Console.ReadLine().Split(' '); + + int[] collArray = new int[coll.Length]; + + for (int i = 0; i < coll.Length; i++) + { + collArray[i] = int.Parse(coll[i]); + } + + string[] windowArr = Console.ReadLine().Split(' '); + + Window_method(collArray, windowArr); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module5/BalancedTree.cs b/CourseApp/Module5/BalancedTree.cs new file mode 100644 index 0000000..307c696 --- /dev/null +++ b/CourseApp/Module5/BalancedTree.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module5 +{ + public class BalancedTree + { + public static void TreeB() + { + var tree = new Tree(); + string[] numbers = Console.ReadLine().Split(' '); + int[] arrNumbers = new int[numbers.Length - 1]; + + for (int i = 0; i < numbers.Length - 1; i++) + { + arrNumbers[i] = int.Parse(numbers[i]); + } + + for (int i = 0; i < arrNumbers.Length; i++) + { + tree.Add(arrNumbers[i]); + } + + if (tree.Balanced()) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + + Console.ReadLine(); + } + } +} diff --git a/CourseApp/Module5/IEquatable.cs b/CourseApp/Module5/IEquatable.cs new file mode 100644 index 0000000..5998df3 --- /dev/null +++ b/CourseApp/Module5/IEquatable.cs @@ -0,0 +1,6 @@ +namespace CourseApp.Module5 +{ + public interface IEquatable + { + } +} \ No newline at end of file diff --git a/CourseApp/Module5/Less1.cs b/CourseApp/Module5/Less1.cs new file mode 100644 index 0000000..2d48680 --- /dev/null +++ b/CourseApp/Module5/Less1.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module5 +{ + public class Less1 + { + public static void TreeW() + { + var tree = new Tree(); + string[] numbers = Console.ReadLine().Split(' '); + int[] arrNumbers = new int[numbers.Length - 1]; + + for (int i = 0; i < numbers.Length - 1; i++) + { + arrNumbers[i] = int.Parse(numbers[i]); + } + + for (int i = 0; i < arrNumbers.Length; i++) + { + tree.Add(arrNumbers[i]); + } + + foreach (var item in tree.Children()) + { + Console.WriteLine(item); + } + + Console.ReadLine(); + } + } +} diff --git a/CourseApp/Module5/Node.cs b/CourseApp/Module5/Node.cs new file mode 100644 index 0000000..df92beb --- /dev/null +++ b/CourseApp/Module5/Node.cs @@ -0,0 +1,72 @@ +using System; + +namespace CourseApp.Module5 +{ + public class Node + where T : IComparable + { + public T Data { get; private set; } + + public Node Left { get; private set; } + + public Node Right { get; private set; } + +#pragma warning disable SA1201 // Elements must appear in the correct order + public Node(T data) +#pragma warning restore SA1201 // Elements must appear in the correct order + { + Data = data; + } + + public Node(T data, Node left, Node right) + { + Data = data; + Left = left; + Right = right; + } + + public bool Add(T data) + { + if (data == null) + { + return false; + } + + var compareResult = data.CompareTo(Data); + + if (compareResult < 0) + { + if (Left == null) + { + Left = new Node(data); + } + else + { + return Left.Add(data); + } + } + else if (compareResult == 0) + { + return false; + } + else + { + if (Right == null) + { + Right = new Node(data); + } + else + { + return Right.Add(data); + } + } + + return true; + } + + public override string ToString() + { + return Data.ToString(); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module5/Tree.cs b/CourseApp/Module5/Tree.cs new file mode 100644 index 0000000..8a84b63 --- /dev/null +++ b/CourseApp/Module5/Tree.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module5 +{ + public class Tree + where T : IComparable + { + public Node Root { get; private set; } + + public int Count { get; private set; } + + public bool Add(T data) + { + if (data == null) + { + return false; + } + + if (Root == null) + { + Root = new Node(data); + Count = 1; + return true; + } + + var addResult = Root.Add(data); + if (addResult) + { + Count++; + } + + return addResult; + } + + public List Preorder() + { + if (Root == null) + { + return new List(); + } + + return Preorder(Root); + } + + public List Children() + { + var list = new List(); + Children(Root, list); + return list; + } + + public bool Balanced() + { + return Balanced(Root); + } + + private List Preorder(Node node) + { + var list = new List(); + if (node != null) + { + list.Add(node.Data); + + if (node.Left != null) + { + list.AddRange(Preorder(node.Left)); + } + + if (node.Right != null) + { + list.AddRange(Preorder(node.Right)); + } + } + + return list; + } + + private void Children(Node node, List list) + { + if (node == null) + { + return; + } + + Children(node.Left, list); + if ((node.Left == null && node.Right != null) || (node.Right == null && node.Left != null)) + { + list.Add(node.Data); + } + + Children(node.Right, list); + } + + private int Height(Node node) + { + if (node == null) + { + return 0; + } + + return 1 + Math.Max(Height(node.Left), Height(node.Right)); + } + + private bool Balanced(Node node) + { + if (node == null) + { + return true; + } + + int leftHeight = Height(node.Left); + int rightHeight = Height(node.Right); + + if (Math.Abs(leftHeight - rightHeight) <= 1 && Balanced(node.Left) && Balanced(node.Right)) + { + return true; + } + + return false; + } + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..6e66598 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,5 +1,5 @@ using System; -using CourseApp.Module2; +using CourseApp.Module5; namespace CourseApp { @@ -7,9 +7,7 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - - Console.WriteLine("Hello World"); + Less1.TreeW(); } } -} +} \ No newline at end of file diff --git a/README.md b/README.md index e58af8a..d7ab9f7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,2 @@ # Tprogramming_42_2020 - -Master branch :) \ No newline at end of file +test \ No newline at end of file diff --git a/work1_1/App.config b/work1_1/App.config new file mode 100644 index 0000000..343984d --- /dev/null +++ b/work1_1/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/work1_1/Program.cs b/work1_1/Program.cs new file mode 100644 index 0000000..f214170 --- /dev/null +++ b/work1_1/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace work1_1 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello"); + } + } +} diff --git a/work1_1/work1_1.csproj b/work1_1/work1_1.csproj new file mode 100644 index 0000000..c8203c2 --- /dev/null +++ b/work1_1/work1_1.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {4A7CE202-E9BB-4DF0-A853-BAB185655D83} + Exe + work1_1 + work1_1 + v2.0 + 512 + false + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file