diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..0d6a8ae --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# SA1201: Elements must appear in the correct order +dotnet_diagnostic.SA1201.severity = none diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index ae46394..27b2938 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + netcoreapp6 True 1573,1591,1701;1702;1705 false diff --git a/CourseApp.Tests/Module2/BubbleSortTest.cs b/CourseApp.Tests/Module2/BubbleSortTest.cs index 8a9b192..c29513a 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 Inp2 = @"4 +1 2 3 4"; + + 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 Out2 = @"0"; public void Dispose() { @@ -24,8 +33,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 +44,12 @@ public void Test1(string input, string expected) Console.SetIn(stringReader); // act - BubbleSort.BubbleSortMethod(); + BubbleSort.Bubble_sort(); // 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/BubbleSort_2Test.cs b/CourseApp.Tests/Module2/BubbleSort_2Test.cs new file mode 100644 index 0000000..a9a9ff6 --- /dev/null +++ b/CourseApp.Tests/Module2/BubbleSort_2Test.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 BubbleSort_2Test : 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 + BubbleSort2.BubbleSort_2(); + + // 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/InversionSortTest.cs b/CourseApp.Tests/Module2/InversionSortTest.cs new file mode 100644 index 0000000..0ed92df --- /dev/null +++ b/CourseApp.Tests/Module2/InversionSortTest.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 InversionSortTest : IDisposable + { + private const string Inp2 = @"2 +3 1"; + + private const string Out2 = @"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(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 + InversionSort.Enter(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/MergeSortTest.cs b/CourseApp.Tests/Module2/MergeSortTest.cs new file mode 100644 index 0000000..03cd208 --- /dev/null +++ b/CourseApp.Tests/Module2/MergeSortTest.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 MergeSortTest : IDisposable + { + private const string Inp1 = @"1 +1"; + + private const string Out1 = @"1"; + + private const string Inp2 = @"2 +3 1"; + + private const string Out2 = @"1 2 1 3 +1 3"; + + private const string Inp3 = @"5 +5 4 3 2 1"; + + private const string Out3 = @"1 2 4 5 +4 5 1 2 +3 5 1 3 +1 5 1 5 +1 2 3 4 5"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + [InlineData(Inp3, Out3)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + MergeSort.Go(); + + // 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/QuickSortTest.cs b/CourseApp.Tests/Module2/QuickSortTest.cs new file mode 100644 index 0000000..62f6fe1 --- /dev/null +++ b/CourseApp.Tests/Module2/QuickSortTest.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 QuickSortTest : 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 + QuickSort.Enter(); + + // 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/RadixSort_Test.cs b/CourseApp.Tests/Module2/RadixSort_Test.cs new file mode 100644 index 0000000..c2932cd --- /dev/null +++ b/CourseApp.Tests/Module2/RadixSort_Test.cs @@ -0,0 +1,81 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class RadixSort_Test : IDisposable + { + private const string Inp1 = @"9 +12 +32 +45 +67 +98 +29 +61 +35 +09"; + + private const string Out1 = @"Initial array: +12, 32, 45, 67, 98, 29, 61, 35, 09 +********** +Phase 1 +Bucket 0: empty +Bucket 1: 61 +Bucket 2: 12, 32 +Bucket 3: empty +Bucket 4: empty +Bucket 5: 45, 35 +Bucket 6: empty +Bucket 7: 67 +Bucket 8: 98 +Bucket 9: 29, 09 +********** +Phase 2 +Bucket 0: 09 +Bucket 1: 12 +Bucket 2: 29 +Bucket 3: 32, 35 +Bucket 4: 45 +Bucket 5: empty +Bucket 6: 61, 67 +Bucket 7: empty +Bucket 8: empty +Bucket 9: 98 +********** +Sorted array: +09, 12, 29, 32, 35, 45, 61, 67, 98"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + RadixSort.Enter(); + + // 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/WarehouseTest.cs b/CourseApp.Tests/Module2/WarehouseTest.cs new file mode 100644 index 0000000..6834488 --- /dev/null +++ b/CourseApp.Tests/Module2/WarehouseTest.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 WarehouseTest : IDisposable + { + private const string Inp1 = @"5 +1 50 3 4 3 +16 +1 2 3 4 5 1 3 3 4 5 5 5 5 5 4 5"; + + private const string Out1 = @"yes +no +no +no +yes"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + Warehouse.Enter(); + + // 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..04b9446 --- /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 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 + Cyclic_Shift.Enter(); + + // 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..3601a18 --- /dev/null +++ b/CourseApp.Tests/Module3/Cyclic_String_Test.cs @@ -0,0 +1,44 @@ +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 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 + Cyclic_String.Enter(); + + // 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_Period_Test.cs b/CourseApp.Tests/Module3/Line_Period_Test.cs new file mode 100644 index 0000000..3a0307b --- /dev/null +++ b/CourseApp.Tests/Module3/Line_Period_Test.cs @@ -0,0 +1,54 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class Line_Period_Test : 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 + Line_Period.Enter(); + + // 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/String_Search_Test.cs b/CourseApp.Tests/Module3/String_Search_Test.cs new file mode 100644 index 0000000..eabc46b --- /dev/null +++ b/CourseApp.Tests/Module3/String_Search_Test.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class String_Search_Test : 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 + String_Search.Enter(); + + // 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/Min_on_Segments_Test.cs b/CourseApp.Tests/Module4/Min_on_Segments_Test.cs new file mode 100644 index 0000000..4e258e6 --- /dev/null +++ b/CourseApp.Tests/Module4/Min_on_Segments_Test.cs @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class Min_on_Segments_Test : 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 + Min_on_Segments.Receive_Min_Segments(); + + // 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_Min_Test.cs b/CourseApp.Tests/Module4/Nearest_Min_Test.cs new file mode 100644 index 0000000..d51248a --- /dev/null +++ b/CourseApp.Tests/Module4/Nearest_Min_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_Min_Test : 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 + Nearest_Min.Receive_Nearest_Min(); + + // 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..614ea54 --- /dev/null +++ b/CourseApp.Tests/Module4/PSP_Test.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class PSP_Test : 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 + PSP.Receive_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/Sorting_Test.cs b/CourseApp.Tests/Module4/Sorting_Test.cs new file mode 100644 index 0000000..3ba0990 --- /dev/null +++ b/CourseApp.Tests/Module4/Sorting_Test.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 Sorting_Test : IDisposable + { + private const string Inp1 = @"3 +3 2 1"; + + private const string Out1 = @"YES"; + + private const string Inp2 = @"4 +4 1 3 2"; + + private const string Out2 = @"YES"; + + private const string Inp3 = @"3 +2 3 1"; + + private const string Out3 = @"NO"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + [InlineData(Inp3, Out3)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + Sorting.Receive_Sort(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module5/Balanced_Tree_Test.cs b/CourseApp.Tests/Module5/Balanced_Tree_Test.cs new file mode 100644 index 0000000..e663d9d --- /dev/null +++ b/CourseApp.Tests/Module5/Balanced_Tree_Test.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.IO; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class Balanced_Tree_Test : 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.BalancedTreeMethod(); + + // 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/Binary_Tree_Test.cs b/CourseApp.Tests/Module5/Binary_Tree_Test.cs new file mode 100644 index 0000000..48296fa --- /dev/null +++ b/CourseApp.Tests/Module5/Binary_Tree_Test.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 Binary_Tree_Test : 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 + Binary_Tree.BinaryTreeMethod(); + + // 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..48a7756 100644 --- a/CourseApp.sln +++ b/CourseApp.sln @@ -7,6 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CourseApp", "CourseApp\Cour EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CourseApp.Tests", "CourseApp.Tests\CourseApp.Tests.csproj", "{34D7AE24-58E8-48D5-A59E-F18CC60525F1}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{708AA578-AA7F-45EA-AB33-CAFE3784417C}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index 9551450..5b415d8 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp2.1 + netcoreapp6 True 1573,1591,1701;1702;1705; diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..87bdcdf 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -1,38 +1,40 @@ using System; -using System.Collections.Generic; -using System.Text; namespace CourseApp.Module2 { public class BubbleSort { - public static void BubbleSortMethod() + public static void Bubble_sort() { - int n = int.Parse(Console.ReadLine()); - string s = Console.ReadLine(); - string[] sValues = s.Split(' '); - int[] arr = new int[n]; - for (int i = 0; i < n; i++) + int mas_L = Convert.ToInt32(Console.ReadLine()); + int[] arr = new int[mas_L]; + bool per = true; + var mas = Console.ReadLine(); + var elems_mas = mas.Split(' '); + + for (int i = 0; i < arr.Length; i++) { - arr[i] = int.Parse(sValues[i]); + arr[i] = int.Parse(elems_mas[i]); } - for (int i = 0; i < arr.Length - 1; i++) + for (int i = 1; i < arr.Length; i++) { - for (int j = 0; j < arr.Length - i - 1; j++) + for (int j = 0; j < arr.Length - i; 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); + per = false; + Console.WriteLine(result); } } } - string result = string.Join(" ", arr); - Console.WriteLine(result); + if (per) + { + Console.WriteLine(Convert.ToInt32(!per)); + } } } } diff --git a/CourseApp/Module2/BubbleSort2.cs b/CourseApp/Module2/BubbleSort2.cs new file mode 100644 index 0000000..c8b64bb --- /dev/null +++ b/CourseApp/Module2/BubbleSort2.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class BubbleSort2 + { + public static void BubbleSort_2() + { + int n = int.Parse(Console.ReadLine()); + int[,] arr = new int[n, 2]; + for (int i = 0; i < n; i++) + { + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + arr[i, 0] = int.Parse(sValues[0]); + arr[i, 1] = int.Parse(sValues[1]); + } + + for (int i = 0; i < (arr.Length / 2) - 1; i++) + { + for (int j = 0; j < (arr.Length / 2) - i - 1; j++) + { + if (arr[j, 1] < arr[j + 1, 1]) + { + (arr[j, 1], arr[j + 1, 1]) = (arr[j + 1, 1], arr[j, 1]); + (arr[j, 0], arr[j + 1, 0]) = (arr[j + 1, 0], arr[j, 0]); + } + else if (arr[j, 1] == arr[j + 1, 1]) + { + if (arr[j, 0] > arr[j + 1, 0]) + { + (arr[j, 0], arr[j + 1, 0]) = (arr[j + 1, 0], arr[j, 0]); + (arr[j, 1], arr[j + 1, 1]) = (arr[j + 1, 1], arr[j, 1]); + } + } + } + } + + for (int i = 0; i < n; i++) + { + Console.WriteLine("{0} {1}", arr[i, 0], arr[i, 1]); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/InversionSort.cs b/CourseApp/Module2/InversionSort.cs new file mode 100644 index 0000000..b076ccd --- /dev/null +++ b/CourseApp/Module2/InversionSort.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class InversionSort + { + private static int count = 0; + + public static int[] Merge(int[] a, int[] b) + { + int i = 0; + int j = 0; + int k = 0; + int[] res = new int[a.Length + b.Length]; + while (k < res.Length) + { + if (j == b.Length || (i < a.Length && a[i] < b[j])) + { + res[k] = a[i]; + i++; + } + else + { + res[k] = b[j]; + j++; + count += a.Length - i; + } + + k++; + } + + return res; + } + + public static int[] Merge_sort(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(v, l, m); + int[] right = Merge_sort(v, m, r); + + int[] sort = Merge(left, right); + + return sort; + } + + public static void Enter() + { + int n = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) + { + arr[i] = int.Parse(sValues[i]); + } + + int[] v_sorted = Merge_sort(arr, 0, n); + + Console.WriteLine("{0}", count); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/MergeSort.cs b/CourseApp/Module2/MergeSort.cs new file mode 100644 index 0000000..6db18db --- /dev/null +++ b/CourseApp/Module2/MergeSort.cs @@ -0,0 +1,73 @@ +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[] Merge(int[] a, int[] b) + { + int i = 0; + int j = 0; + int k = 0; + int[] res = new int[a.Length + b.Length]; + while (k < res.Length) + { + if (j == b.Length || (i < a.Length && a[i] < b[j])) + { + res[k] = a[i]; + i++; + } + else + { + res[k] = b[j]; + j++; + } + + k++; + } + + return res; + } + + public static int[] Merge_sort(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(v, l, m); + int[] right = Merge_sort(v, m, r); + + int[] sort = Merge(left, right); + + Console.WriteLine("{0} {1} {2} {3}", l + 1, r, sort[0], sort[^1]); + + return Merge(left, right); + } + + public static void Go() + { + int n = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) + { + arr[i] = int.Parse(sValues[i]); + } + + int[] v_sorted = Merge_sort(arr, 0, n); + + Console.WriteLine("{0}", string.Join(" ", v_sorted)); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/QuickSort.cs b/CourseApp/Module2/QuickSort.cs new file mode 100644 index 0000000..20b0371 --- /dev/null +++ b/CourseApp/Module2/QuickSort.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 QuickSort + { + private static long count = 1; + + public static int Partition(int[] mas, int l, int r) + { + int x = mas[l]; + int i = l - 1, j = r + 1; + + while (true) + { + do + { + i++; + } + while (mas[i] < x); + + do + { + j--; + } + while (mas[j] > x); + + if (i >= j) + { + return j; + } + + int temp = mas[i]; + mas[i] = mas[j]; + mas[j] = temp; + } + } + + public static void QSort(int[] mas, int l, int r) + { + if (l < r) + { + int q = Partition(mas, l, r); + + QSort(mas, l, q); + QSort(mas, q + 1, r); + } + } + + public static void Enter() + { + int n = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + int[] mas = new int[n]; + for (int i = 0; i < n; i++) + { + mas[i] = int.Parse(sValues[i]); + } + + QSort(mas, 0, n - 1); + + for (int i = 1; i < n; i++) + { + if (mas[i - 1] != mas[i]) + { + count += 1; + } + } + + Console.WriteLine("{0}", count); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/RadixSort.cs b/CourseApp/Module2/RadixSort.cs new file mode 100644 index 0000000..1beb110 --- /dev/null +++ b/CourseApp/Module2/RadixSort.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace CourseApp.Module2 +{ + public class RadixSort + { + public static string[] Sort(string[] mas_string, int phase, int length) + { + ulong x; + List[] mas = new List[10]; + for (int i = 0; i < 10; i++) + { + mas[i] = new List(); + } + + for (int j = 0; j < mas_string.Length; j++) + { + int s = int.Parse(mas_string[j].Substring(length - phase, 1)); + mas[s].Add(mas_string[j]); + } + + for (x = 0; x < 10; x++) + { + if (mas[x].Count == 0) + { + Console.WriteLine("Bucket " + x + ": empty"); + } + else + { + Console.WriteLine("Bucket " + x + ": {0}", string.Join(", ", mas[x])); + } + } + + int a = 0; + + for (x = 0; x < 10; x++) + { + for (int j = 0; j < mas[x].Count; j++) + { + mas_string[a] = mas[x][j]; + a++; + } + } + + return mas_string; + } + + public static void RadixSortMethod(string[] mas_string, ulong value) + { + int phase = 1; + int rank = mas_string[0].Length; + + Console.WriteLine("Initial array:"); + Console.WriteLine("{0}", string.Join(", ", mas_string)); + + foreach (var i in Enumerable.Range(0, Convert.ToInt32(Math.Ceiling(Convert.ToDouble(-1 - (rank - 1)) / -1))).Select(g => rank - 1 + (g * -1))) + { + Console.WriteLine("**********"); + Console.WriteLine("Phase {0}", phase); + mas_string = Sort(mas_string, phase, rank); + phase++; + } + + Console.WriteLine("**********"); + Console.WriteLine("Sorted array:"); + Console.Write("{0}", string.Join(", ", mas_string)); + } + + public static void Enter() + { + ulong y = ulong.Parse(Console.ReadLine()); + string[] arr_string = new string[y]; + for (ulong i = 0; i < y; i++) + { + arr_string[i] = Console.ReadLine(); + } + + RadixSortMethod(arr_string, y); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/Warehouse.cs b/CourseApp/Module2/Warehouse.cs new file mode 100644 index 0000000..73fd4b5 --- /dev/null +++ b/CourseApp/Module2/Warehouse.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 Warehouse + { + public static void Warehoust_Sort(int[] assignment_mas, int[] mas_of_products, int k) + { + int[] w = new int[k + 1]; + for (int i = 0; i < assignment_mas.Length; i++) + { + w[assignment_mas[i]]++; + } + + int a = 0; + int b = 0; + int[] mas_of_orders = new int[k]; + string[] answer = new string[k]; + for (int i = 0; i < w.Length; i++) + { + if (w[i] != 0) + { + mas_of_orders[a++] = w[i]; + if (mas_of_products[b] >= mas_of_orders[b]) + { + Console.WriteLine("no"); + } + else + { + Console.WriteLine("yes"); + } + + b++; + } + } + } + + public static void Enter() + { + int products = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + int[] mas_of_products = new int[products]; + for (int i = 0; i < products; i++) + { + mas_of_products[i] = int.Parse(sValues[i]); + } + + int n = int.Parse(Console.ReadLine()); + s = Console.ReadLine(); + sValues = s.Split(' '); + int[] assignment_mas = new int[n]; + for (int i = 0; i < n; i++) + { + assignment_mas[i] = int.Parse(sValues[i]); + } + + Warehoust_Sort(assignment_mas, mas_of_products, products); + } + } +} \ 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..155b659 --- /dev/null +++ b/CourseApp/Module3/Cyclic_Shift.cs @@ -0,0 +1,74 @@ +using System; +using System.Linq; + +namespace CourseApp.Module3 +{ + public class Cyclic_Shift + { + public static int Metod_RK(string s, string a) + { + if (s == a) + { + return 0; + } + + a = string.Concat(Enumerable.Repeat(a, 2)); + + long z = 13; + long x = 1; + long p = 7777777; + + long fir_h = 0; + long sec_h = 0; + long xa = 1; + + foreach (char i in s.Reverse()) + { + fir_h = (fir_h + (i * x)) % p; + x = (x * z) % p; + } + + x = 1; + + for (int i = s.Length - 1; i >= 0; i--) + { + sec_h = (sec_h + (a[i] * x)) % p; + x = (x * z) % p; + } + + for (int i = 0; i < s.Length - 1; i++) + { + xa = (xa * z) % p; + } + + for (int i = 1; i < a.Length - s.Length + 1; i++) + { + if (fir_h == sec_h) + { + return i - 1; + } + + sec_h = z * (sec_h - (a[i - 1] * xa)); + sec_h += a[i + s.Length - 1]; + sec_h = sec_h % p; + + if ((sec_h < 0 && p > 0) || (sec_h > 0 && p < 0)) + { + sec_h += p; + } + } + + return -1; + } + + public static void Enter() + { + string s = Console.ReadLine(); + string a = Console.ReadLine(); + + int result = Metod_RK(s, a); + + Console.WriteLine(result); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/Cyclic_String.cs b/CourseApp/Module3/Cyclic_String.cs new file mode 100644 index 0000000..2d15767 --- /dev/null +++ b/CourseApp/Module3/Cyclic_String.cs @@ -0,0 +1,45 @@ +using System; + +namespace CourseApp.Module3 +{ + public class Cyclic_String + { + public static int[] Prefix_Metod(string s) + { + int[] result = new int[s.Length]; + result[0] = 0; + + for (int i = 0; i < s.Length - 1; i++) + { + int j = result[i]; + + while (j > 0 && s[i + 1] != s[j]) + { + j = result[j - 1]; + } + + if (s[i + 1] == s[j]) + { + result[i + 1] = j + 1; + } + else + { + result[i + 1] = 0; + } + } + + return result; + } + + public static void Enter() + { + string s = Console.ReadLine(); + + int[] pref = Prefix_Metod(s); + + int result = s.Length - pref[s.Length - 1]; + + Console.WriteLine(result); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/Line_Period.cs b/CourseApp/Module3/Line_Period.cs new file mode 100644 index 0000000..3eafa3a --- /dev/null +++ b/CourseApp/Module3/Line_Period.cs @@ -0,0 +1,52 @@ +using System; + +namespace CourseApp.Module3 +{ + public class Line_Period + { + public static int[] Prefix_Metod(string s) + { + int[] result = new int[s.Length]; + result[0] = 0; + + for (int i = 0; i < s.Length - 1; i++) + { + int j = result[i]; + + while (j > 0 && s[i + 1] != s[j]) + { + j = result[j - 1]; + } + + if (s[i + 1] == s[j]) + { + result[i + 1] = j + 1; + } + else + { + result[i + 1] = 0; + } + } + + return result; + } + + public static void Enter() + { + string s = Console.ReadLine(); + + int[] pref = Prefix_Metod(s); + + int result = s.Length - pref[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/String_Search.cs b/CourseApp/Module3/String_Search.cs new file mode 100644 index 0000000..f159600 --- /dev/null +++ b/CourseApp/Module3/String_Search.cs @@ -0,0 +1,57 @@ +using System; + +namespace CourseApp.Module3 +{ + public class String_Search + { + public static long Get(string s, int z, int y, int x) + { + long res = 0; + for (int i = 0; i < z; i++) + { + res = ((res * x) + s[i]) % y; + } + + return res; + } + + public static void Metod_Rabin_Karp(string s, string a, int y, int x) + { + long ha = Get(a, a.Length, y, x); + + long hs = Get(s, a.Length, y, x); + + long xt = 1; + + for (int i = 0; i < a.Length; i++) + { + xt = (xt * x) % y; + } + + for (int i = 0; i <= s.Length - a.Length; i++) + { + if (ha == hs) + { + Console.Write("{0} ", i); + } + + if (i + a.Length < s.Length) + { + hs = ((hs * x) - (s[i] * xt) + s[i + a.Length]) % y; + hs = (hs + y) % y; + } + } + } + + public static void Enter() + { + string s = Console.ReadLine(); + string a = Console.ReadLine(); + + int y = 77777; + int x = 26; + + Metod_Rabin_Karp(s, a, y, x); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/Min_on_Segments.cs b/CourseApp/Module4/Min_on_Segments.cs new file mode 100644 index 0000000..bcc42ea --- /dev/null +++ b/CourseApp/Module4/Min_on_Segments.cs @@ -0,0 +1,138 @@ +using System; + +public class Min_on_Segments +{ + public static void Receive_Min_Segments() + { + string first = Console.ReadLine(); + string[] first_values = first.Split(' '); + int[] first_mas = new int[2]; + for (int i = 0; i < 2; i++) + { + first_mas[i] = int.Parse(first_values[i]); + } + + string second = Console.ReadLine(); + string[] second_values = second.Split(' '); + int[] second_mas = new int[first_mas[0]]; + for (int i = 0; i < first_mas[0]; i++) + { + second_mas[i] = int.Parse(second_values[i]); + } + + for (int i = 0; i < first_mas[1]; i++) + { + while (Deque.Empty() == false && second_mas[i] < second_mas[Deque.Front()]) + { + Deque.Pop_Front(); + } + + Deque.Push_Front(i); + } + + for (int i = first_mas[1]; i < first_mas[0]; i++) + { + Console.WriteLine(second_mas[Deque.Back()]); + + while (Deque.Empty() == false && Deque.Back() <= i - first_mas[1]) + { + Deque.Pop_Back(); + } + + while (Deque.Empty() == false && second_mas[i] < second_mas[Deque.Front()]) + { + Deque.Pop_Front(); + if (Deque.Size() == 0) + { + Deque.Clear(); + } + } + + Deque.Push_Front(i); + } + + Console.WriteLine(second_mas[Deque.Back()]); + } + + private class Deque + { + private static int w = 777777777; + private static int[] buffer = new int[w]; + private static int front = 0; + private static int back = w - 1; + private static int size = 0; + + public static void Push_Back(int t) + { + back++; + if (back == w) + { + back = 0; + } + + buffer[back] = t; + size++; + } + + public static void Push_Front(int t) + { + front--; + if (front < 0) + { + front = w - 1; + } + + buffer[front] = t; + size++; + } + + public static void Pop_Back() + { + back--; + if (back < 0) + { + back = w - 1; + } + + size--; + } + + public static void Pop_Front() + { + front++; + if (front == w) + { + front = 0; + } + + size--; + } + + public static void Clear() + { + front = 0; + back = w - 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; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/Nearest_Min.cs b/CourseApp/Module4/Nearest_Min.cs new file mode 100644 index 0000000..6fe00ef --- /dev/null +++ b/CourseApp/Module4/Nearest_Min.cs @@ -0,0 +1,81 @@ +using System; + +public class Nearest_Min +{ + public static void Receive_Nearest_Min() + { + var elem_num = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var sValues = s.Split(' '); + var elem_mas = new int[elem_num]; + var mas_answer = new int[elem_num]; + for (int i = 0; i < elem_num; i++) + { + elem_mas[i] = int.Parse(sValues[i]); + } + + int index = elem_num - 1; + while (index >= 0) + { + while (Stack.Empty() == false && elem_mas[Stack.Back()] >= elem_mas[index]) + { + Stack.Pop(); + } + + if (Stack.Empty() == true) + { + mas_answer[index] = -1; + } + else + { + mas_answer[index] = Stack.Back(); + } + + Stack.Push(index); + + index--; + } + + for (int i = 0; i < elem_num; i++) + { + Console.Write(mas_answer[i] + " "); + } + } + + private class Stack + { + private static int[] buf = new int[100000000 + 100000]; + private static int top = -1; + + public static void Push(int a) + { + top++; + buf[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 buf[top]; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/PSP.cs b/CourseApp/Module4/PSP.cs new file mode 100644 index 0000000..230ead1 --- /dev/null +++ b/CourseApp/Module4/PSP.cs @@ -0,0 +1,68 @@ +using System; + +namespace CourseApp.Module4 +{ + public class PSP + { + public static void Receive_PSP() + { + string bracket = Console.ReadLine(); + int c = 0; + + for (int i = 0; i < bracket.Length; i++) + { + if (bracket[i] == '(' || bracket[i] == '{' || bracket[i] == '[') + { + Stack.Push(bracket[i]); + } + else if (Stack.Empty() == false && (bracket[i] == ')' || bracket[i] == '}' || bracket[i] == ']')) + { + Stack.Pop(); + } + else + { + c += 1; + } + } + + Console.WriteLine(c + Stack.Size()); + } + + private class Stack + { + private static int[] buf = new int[100000000 + 100000]; + private static int top = -1; + + public static void Push(int a) + { + top++; + buf[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 buf[top]; + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/Sorting.cs b/CourseApp/Module4/Sorting.cs new file mode 100644 index 0000000..f21d5ab --- /dev/null +++ b/CourseApp/Module4/Sorting.cs @@ -0,0 +1,94 @@ +using System; + +namespace CourseApp.Module4 +{ + public class Sorting + { + public static void Receive_Sort() + { + var wagons_num = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var sValues = s.Split(' '); + var wagons_mas = new int[wagons_num]; + var answer_mas = new int[wagons_num]; + for (int i = 0; i < wagons_num; i++) + { + wagons_mas[i] = int.Parse(sValues[i]); + } + + int a = 0; + int b = 0; + + while (a != wagons_num) + { + if (Stack.Empty() == true || (b < wagons_num && wagons_mas[b] < Stack.Back())) + { + Stack.Push(wagons_mas[b]); + b++; + } + else + { + answer_mas[a] += Stack.Back(); + Stack.Pop(); + a++; + } + } + + bool answer = true; + + for (int i = 0; i < answer_mas.Length - 1; i++) + { + if (answer_mas[i] > answer_mas[i + 1]) + { + answer = false; + } + } + + if (answer == true) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + private class Stack + { + private static int[] buf = new int[100000000 + 100000]; + private static int top = -1; + + public static void Push(int a) + { + top++; + buf[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 buf[top]; + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module5/Balanced_Tree.cs b/CourseApp/Module5/Balanced_Tree.cs new file mode 100644 index 0000000..9c93193 --- /dev/null +++ b/CourseApp/Module5/Balanced_Tree.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module5 +{ + public class Balanced_Tree + { + private Node root; + + public static void BalancedTreeMethod() + { + 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])); + } + + tree.Check(); + } + + public void Insert(int data) + { + root = InnerInsert(data, root); + } + + private Node InnerInsert(int data, Node root) + { + if (root == null) + { + return new Node(data); + } + + if (root.Data > data) + { + root.Left = InnerInsert(data, root.Left); + } + else if (root.Data < data) + { + root.Right = InnerInsert(data, root.Right); + } + + return root; + } + + private bool CheckBalancedTree(Node node) + { + if (node == null) + { + return true; + } + + int righttree = Level(node.Left); + int lefttree = Level(node.Right); + + if (Math.Abs(lefttree - righttree) <= 1 && CheckBalancedTree(node.Left) && CheckBalancedTree(node.Right)) + { + return true; + } + + return false; + } + + private int Level(Node node) + { + if (node == null) + { + return 0; + } + + return 1 + Math.Max(Level(node.Left), Level(node.Right)); + } + + private void Check() + { + if (CheckBalancedTree(root)) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + private void InnerTraversal(Node node) + { + if (node == null) + { + return; + } + + InnerTraversal(node.Left); + InnerTraversal(node.Right); + } + + internal class Node + { + public Node(int v) + { + Data = v; + Left = null; + Right = null; + } + + public Node Left { get; set; } + + public Node Right { get; set; } + + public int Data { get; set; } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module5/Binary_Tree.cs b/CourseApp/Module5/Binary_Tree.cs new file mode 100644 index 0000000..4e6c6f4 --- /dev/null +++ b/CourseApp/Module5/Binary_Tree.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module5 +{ + public class Binary_Tree + { + private Node root; + private List size; + + public static void BinaryTreeMethod() + { + string s = Console.ReadLine(); + + string[] sValues = s.Split(' '); + + var tree = new Binary_Tree(); + + for (int i = 0; i < sValues.Length - 1; i++) + { + tree.Insert(int.Parse(sValues[i])); + } + + tree.FindLastElem(); + } + + public void Insert(int v) + { + root = InnerInsert(v, root); + } + + public List GetValues() + { + size = new List(); + InnerTraversal(root); + return size; + } + + private bool InnerFind(int v, Node root) + { + if (root == null) + { + return false; + } + + if (v == root.Data) + { + return true; + } + else if (v < root.Data) + { + return InnerFind(v, root.Left); + } + else + { + return InnerFind(v, root.Right); + } + } + + private void FindLastElem() + { + LastElem(root); + } + + private void LastElem(Node value) + { + if (value == null) + { + return; + } + + LastElem(value.Left); + + if ((value.Left != null && value.Right == null) || (value.Right != null && value.Left == null)) + { + Console.WriteLine(value.Data); + } + + LastElem(value.Right); + } + + private Node InnerInsert(int v, Node root) + { + if (root == null) + { + return new Node(v); + } + + if (root.Data > v) + { + root.Left = InnerInsert(v, root.Left); + } + else if (root.Data < v) + { + root.Right = InnerInsert(v, root.Right); + } + + return root; + } + + private void InnerTraversal(Node node) + { + if (node == null) + { + return; + } + + InnerTraversal(node.Left); + size.Add(node.Data); + InnerTraversal(node.Right); + } + + internal class Node + { + public Node(int v) + { + Data = v; + Left = null; + Right = null; + } + + public Node Left { get; set; } + + public Node Right { get; set; } + + public int Data { get; set; } + } + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..08ce092 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -7,9 +7,18 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - - Console.WriteLine("Hello World"); + // BubbleSort.Bubble_sort(); // + // BubbleSort2.BubbleSort_2(); // + // MergeSort.Go(); // + // InversionSort.Enter(); // + // QuickSort.Enter(); // + // Warehouse.Enter(); // + // PSP.Receive_PSP(); // + // Sorting.Receive_Sort(); // + // Nearest_Min.Receive_Nearest_Min(); // + // Binary_Tree.BinaryTreeMethod(); // + // Balanced_Tree.BalancedTreeMethod(); // + RadixSort.Enter(); } } } diff --git a/README.md b/README.md index e58af8a..402e290 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,2 @@ # Tprogramming_42_2020 - -Master branch :) \ No newline at end of file +Sokolov Vladimir \ No newline at end of file