diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..742612d --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/CourseApp/CourseApp.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/CourseApp/CourseApp.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/CourseApp/CourseApp.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index ae46394..32a1161 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + net6.0 True 1573,1591,1701;1702;1705 false diff --git a/CourseApp.Tests/Module2/BitwiseSortingTest.cs b/CourseApp.Tests/Module2/BitwiseSortingTest.cs new file mode 100644 index 0000000..76f153f --- /dev/null +++ b/CourseApp.Tests/Module2/BitwiseSortingTest.cs @@ -0,0 +1,81 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class BitwiseSortingTest : 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 standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + BitwiseSorting.BitwiseSorting_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module2/BubbleSortTest.cs b/CourseApp.Tests/Module2/BubbleSortTest.cs index 8a9b192..2ce14ed 100644 --- a/CourseApp.Tests/Module2/BubbleSortTest.cs +++ b/CourseApp.Tests/Module2/BubbleSortTest.cs @@ -8,24 +8,33 @@ namespace CourseApp.Tests.Module2 [Collection("Sequential")] public class BubbleSortTest : IDisposable { - private const string Inp1 = @"7 -5 1 7 3 9 4 1"; + private const string Inp1 = @"3 +1 2 3"; - private const string Inp2 = @"3 --10 7 2"; + private const string Out1 = @"0"; + + private const string Inp2 = @"4 +4 3 2 1"; + + private const string Out2 = @"3 4 2 1 +3 2 4 1 +3 2 1 4 +2 3 1 4 +2 1 3 4 +1 2 3 4"; public void Dispose() { + var standardIn = new StreamReader(Console.OpenStandardInput()); var standardOut = new StreamWriter(Console.OpenStandardOutput()); standardOut.AutoFlush = true; - var standardIn = new StreamReader(Console.OpenStandardInput()); Console.SetOut(standardOut); Console.SetIn(standardIn); } [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 +48,8 @@ public void Test1(string input, string expected) // assert var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - Assert.Equal($"{expected}", output[0]); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); } } } diff --git a/CourseApp.Tests/Module2/InverseNumTest.cs b/CourseApp.Tests/Module2/InverseNumTest.cs new file mode 100644 index 0000000..ef9d3ca --- /dev/null +++ b/CourseApp.Tests/Module2/InverseNumTest.cs @@ -0,0 +1,69 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class InverseNumTest : IDisposable + { + private const string Inp1 = @"1 +1"; + + private const string Inp2 = @"2 +3 1"; + + private const string Out1 = @"0"; + + private const string Out2 = @"1"; + /* private const string Inp1 = @"1 + 1"; + + private const string Out1 = @"0"; + + private const string Inp2 = @"2 + 3 1"; + + private const string Out2 = @"1";*/ + + /* private const string Inp3 = @"5 + 5 4 3 2 1"; + + private const string Out3 = @"10";*/ + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + InverseNum.InverseNumMain(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module2/MergeSortTest.cs b/CourseApp.Tests/Module2/MergeSortTest.cs new file mode 100644 index 0000000..a72f486 --- /dev/null +++ b/CourseApp.Tests/Module2/MergeSortTest.cs @@ -0,0 +1,61 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class MergeSortTest : IDisposable + { + private const string Inp1 = @"1 +1"; + + private const string Out1 = @"1"; + + private const string Inp2 = @"2 +3 1"; + + private const string Out2 = @"1 2 1 3 +1 3"; + + private const string Inp3 = @"5 +5 4 3 2 1"; + + private const string Out3 = @"1 2 4 5 +4 5 1 2 +3 5 1 3 +1 5 1 5 +1 2 3 4 5"; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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.MergeSortMain(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module2/NumOfDiffTest.cs b/CourseApp.Tests/Module2/NumOfDiffTest.cs new file mode 100644 index 0000000..75e3744 --- /dev/null +++ b/CourseApp.Tests/Module2/NumOfDiffTest.cs @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class NumOfDiffTest : IDisposable + { + private const string Inp1 = @"5 +1 0 1 2 0 +"; + + private const string Out1 = @"3"; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + NumOfDiff.NumOfDiffMain(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module2/SortingPairsTest.cs b/CourseApp.Tests/Module2/SortingPairsTest.cs new file mode 100644 index 0000000..06b8e1b --- /dev/null +++ b/CourseApp.Tests/Module2/SortingPairsTest.cs @@ -0,0 +1,64 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class SortingPairsTest : IDisposable + { + private const string Inp1 = @"3 +101 80 +305 90 +200 14 +"; + + private const string Out1 = @"305 90 +101 80 +200 14"; + + private const string Inp2 = @"3 +20 80 +30 90 +25 90 +"; + + private const string Out2 = @"25 90 +30 90 +20 80"; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + SortingPairs.SortingPairsMetod(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module2/WarehouseTest.cs b/CourseApp.Tests/Module2/WarehouseTest.cs new file mode 100644 index 0000000..e3ac2fd --- /dev/null +++ b/CourseApp.Tests/Module2/WarehouseTest.cs @@ -0,0 +1,55 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + 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 standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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.WarehouseMain(); + + // 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/CyclicShiftTest.cs b/CourseApp.Tests/Module3/CyclicShiftTest.cs new file mode 100644 index 0000000..92ca3ec --- /dev/null +++ b/CourseApp.Tests/Module3/CyclicShiftTest.cs @@ -0,0 +1,52 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class CyclicShiftTest : IDisposable + { + private const string Inp1 = @"a +b"; + + private const string Out1 = @"-1"; + private const string Inp2 = @"zabcd +abcdz"; + + private const string Out2 = @"4"; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + CyclicShift.CyclicShift_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module3/CyclicStringTest.cs b/CourseApp.Tests/Module3/CyclicStringTest.cs new file mode 100644 index 0000000..17d7a80 --- /dev/null +++ b/CourseApp.Tests/Module3/CyclicStringTest.cs @@ -0,0 +1,47 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class CyclicStringTest : IDisposable + { + private const string Inp1 = @"z"; + + private const string Out1 = @"1"; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + CyclicString.CyclicString_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module3/StringPeriodTest.cs b/CourseApp.Tests/Module3/StringPeriodTest.cs new file mode 100644 index 0000000..f957df1 --- /dev/null +++ b/CourseApp.Tests/Module3/StringPeriodTest.cs @@ -0,0 +1,53 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class StringPeriodTest : 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 standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + StringPeriod.StringPeriod_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module3/SubstringSearchTest.cs b/CourseApp.Tests/Module3/SubstringSearchTest.cs new file mode 100644 index 0000000..6951667 --- /dev/null +++ b/CourseApp.Tests/Module3/SubstringSearchTest.cs @@ -0,0 +1,48 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class SubstringSearchTest : IDisposable + { + private const string Inp1 = @"ababbababa +aba"; + + private const string Out1 = @"0 5 7"; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + SubstringSearch.SubstringSearch_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module4/GetPSPTest.cs b/CourseApp.Tests/Module4/GetPSPTest.cs new file mode 100644 index 0000000..db9ebc5 --- /dev/null +++ b/CourseApp.Tests/Module4/GetPSPTest.cs @@ -0,0 +1,51 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class GetPSPTest : IDisposable + { + private const string Inp1 = @"())(()"; + + private const string Out1 = @"2"; + + private const string Inp2 = @"))((("; + + private const string Out2 = @"5"; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + GetPSP.GetPSP_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module4/MinOnSegmTest.cs b/CourseApp.Tests/Module4/MinOnSegmTest.cs new file mode 100644 index 0000000..ad7911e --- /dev/null +++ b/CourseApp.Tests/Module4/MinOnSegmTest.cs @@ -0,0 +1,52 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class MinOnSegmTest : 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 standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + MinOnSegm.MinOnSegm_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module4/NearestSmallerTest.cs b/CourseApp.Tests/Module4/NearestSmallerTest.cs new file mode 100644 index 0000000..4428f90 --- /dev/null +++ b/CourseApp.Tests/Module4/NearestSmallerTest.cs @@ -0,0 +1,48 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class NearestSmallerTest : 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 standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + NearestSmaller.NearestSmaller_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module4/SortTest.cs b/CourseApp.Tests/Module4/SortTest.cs new file mode 100644 index 0000000..8c25ace --- /dev/null +++ b/CourseApp.Tests/Module4/SortTest.cs @@ -0,0 +1,58 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class SortTest : IDisposable + { + private const string Inp1 = @"3 +3 2 1"; + + private const string Out1 = @"YES"; + + private const string Inp2 = @"4 +4 1 3 2"; + + private const string Out2 = @"YES"; + + private const string Inp3 = @"3 +2 3 1"; + + private const string Out3 = @"NO"; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + Sort.Sort_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module5/IsTreeBalancedTest.cs b/CourseApp.Tests/Module5/IsTreeBalancedTest.cs new file mode 100644 index 0000000..040480a --- /dev/null +++ b/CourseApp.Tests/Module5/IsTreeBalancedTest.cs @@ -0,0 +1,47 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class IsTreeBalancedTest : IDisposable + { + private const string Inp1 = @"7 3 2 1 9 5 4 6 8 0"; + + private const string Out1 = @"YES"; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + IsTreeBalanced.IsTreeBalanced_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module5/KSubSectionTest.cs b/CourseApp.Tests/Module5/KSubSectionTest.cs new file mode 100644 index 0000000..f343c25 --- /dev/null +++ b/CourseApp.Tests/Module5/KSubSectionTest.cs @@ -0,0 +1,54 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class KSubSectionTest : IDisposable + { + private const string Inp1 = @"5 +0 0 2 0 1 +5 +1 5 2 +2 4 2 +3 5 3 +1 1 1 +3 4 1"; + + private const string Out1 = @"2 4 -1 1 4 "; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + KSubSection.KSubSection_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module5/KSubSelectionDynamicTest.cs b/CourseApp.Tests/Module5/KSubSelectionDynamicTest.cs new file mode 100644 index 0000000..8bbf261 --- /dev/null +++ b/CourseApp.Tests/Module5/KSubSelectionDynamicTest.cs @@ -0,0 +1,52 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class KSubSelectionDynamicTest : IDisposable + { + private const string Inp1 = @"5 +0 0 3 0 2 +3 +u 1 5 +u 1 0 +s 1 5 3"; + + private const string Out1 = @"4 "; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + KSubSelectionDynamic.KSubSelectionDynamic_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module5/NODSubSectionDynamicTest.cs b/CourseApp.Tests/Module5/NODSubSectionDynamicTest.cs new file mode 100644 index 0000000..0a6c9df --- /dev/null +++ b/CourseApp.Tests/Module5/NODSubSectionDynamicTest.cs @@ -0,0 +1,54 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class NODSubSectionDynamicTest : IDisposable + { + private const string Inp1 = @"5 +2 8 4 16 12 +5 +s 1 5 +s 4 5 +u 3 32 +s 2 5 +s 3 3"; + + private const string Out1 = @"2 4 4 32 "; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + NODSubSectionDynamic.NODSubSectionDynamic_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module5/NODSubSectionsTest.cs b/CourseApp.Tests/Module5/NODSubSectionsTest.cs new file mode 100644 index 0000000..a83ce8f --- /dev/null +++ b/CourseApp.Tests/Module5/NODSubSectionsTest.cs @@ -0,0 +1,51 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class NODSubSectionsTest : IDisposable + { + private const string Inp1 = @"5 +2 2 2 1 5 +2 +2 3 +2 5"; + + private const string Out1 = @"2 1 "; + + public void Dispose() + { + var standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + NODSubSections.NODSubSections_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp.Tests/Module5/OutBinaryTreeTest.cs b/CourseApp.Tests/Module5/OutBinaryTreeTest.cs new file mode 100644 index 0000000..59ce947 --- /dev/null +++ b/CourseApp.Tests/Module5/OutBinaryTreeTest.cs @@ -0,0 +1,48 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class OutBinaryTreeTest : 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 standardIn = new StreamReader(Console.OpenStandardInput()); + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + 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 + OutBinaryTree.OutBinaryTree_Main(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + Assert.Equal($"{expected}", result); + } + } +} diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index 9551450..e6f1a1b 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -2,9 +2,10 @@ Exe - netcoreapp2.1 - True + net6.0 + False 1573,1591,1701;1702;1705; + diff --git a/CourseApp/Module2/BitwiseSorting.cs b/CourseApp/Module2/BitwiseSorting.cs new file mode 100644 index 0000000..89b4fb9 --- /dev/null +++ b/CourseApp/Module2/BitwiseSorting.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class BitwiseSorting + { + private static void BitwiseSortingMetod(string[] arrStr) + { + Console.WriteLine("Initial array:"); + Console.WriteLine(string.Join(", ", arrStr)); + int phase = 1; + int round = arrStr[0].Length; + + for (int i = round - 1; i >= 0; i--) + { + Console.WriteLine("**********"); + Console.WriteLine($"Phase {phase}"); + Sort(arrStr, phase, round); + phase++; + } + Console.WriteLine("**********"); + Console.WriteLine("Sorted array:"); + Console.WriteLine(String.Join(", ", arrStr)); + + } + + private static string[] Sort(string[] arrStr, int phase, int round) + { + List[] arrSorted = new List[10]; + for (int i = 0; i < 10; i++) + { + arrSorted[i] = new List(); + } + + for (int j = 0; j < arrStr.Length; j++) + { + int x = int.Parse(arrStr[j].Substring(round - phase, 1)); + arrSorted[x].Add(arrStr[j]); + } + + for (int i = 0; i < 10; i++) + { + if (arrSorted[i].Count == 0) + { + Console.WriteLine($"Bucket {i}: empty"); + } + else + { + Console.WriteLine($"Bucket {i}: " + string.Join(", ", arrSorted[i])); + } + } + int p = 0; + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < arrSorted[i].Count; j++) + { + arrStr[p] = arrSorted[i][j]; + p++; + } + } + + return arrStr; + } + public static void BitwiseSorting_Main() + { + int n = int.Parse(Console.ReadLine()); + string[] arrStr = new string[n]; + for (int i = 0; i < arrStr.Length; i++) + { + arrStr[i] = Console.ReadLine(); + } + BitwiseSortingMetod(arrStr); + } + } +} diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..d47d9a2 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -8,31 +8,38 @@ public class BubbleSort { public static void BubbleSortMethod() { - int n = int.Parse(Console.ReadLine()); - string s = Console.ReadLine(); - string[] sValues = s.Split(' '); - int[] arr = new int[n]; - for (int i = 0; i < n; i++) + int numbers = int.Parse(Console.ReadLine()); + string str = Console.ReadLine(); + string[] arrayStr = str.Split(' '); + int[] array = new int[numbers]; + bool sTrue = false; + for (int i = 0; i < numbers; i++) { - arr[i] = int.Parse(sValues[i]); + array[i] = int.Parse(arrayStr[i]); } - for (int i = 0; i < arr.Length - 1; i++) + for (int i = 0; i < numbers - 1; i++) { - for (int j = 0; j < arr.Length - i - 1; j++) + for (int j = 0; j < numbers - i - 1; j++) { - if (arr[j] > arr[j + 1]) + if (array[j] > array[j + 1]) { - // int temp = arr[j]; - // arr[j] = arr[j + 1]; - // arr[j+1] = temp; - (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]); + (array[j], array[j + 1]) = (array[j + 1], array[j]); + Console.WriteLine(string.Join(" ", array)); + sTrue = true; } } } - string result = string.Join(" ", arr); - Console.WriteLine(result); + if (sTrue == false) + { + Console.WriteLine(0); + } } + + // public static void Main() + // { + // BubbleSortMethod(); + // } } } diff --git a/CourseApp/Module2/InverseNum.cs b/CourseApp/Module2/InverseNum.cs new file mode 100644 index 0000000..c0cc731 --- /dev/null +++ b/CourseApp/Module2/InverseNum.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class InverseNum + { + private static long count = 0; + + public static int[] Merge(int[] left, int[] right) + { + int i = 0, j = 0; + int[] c = new int[left.Length + right.Length]; + for (int k = 0; k < c.Length; k++) + { + if (j == right.Length) + { + c[k] = left[i]; + i++; + } + else if (i == left.Length) + { + c[k] = right[j]; + j++; + } + else if (left[i] <= right[j]) + { + c[k] = left[i]; + i++; + } + else + { + c[k] = right[j]; + j++; + count += left.Length - i; + } + } + + return c; + } + + public static int[] MergeSort(int[] num, int l, int r) + { + if ((r - l) == 1) + { + int[] res = new int[1]; + res[0] = num[l]; + return res; + } + + int m = (l + r) / 2; + + int[] left = MergeSort(num, l, m); + int[] right = MergeSort(num, m, r); + int[] sort = Merge(left, right); + return sort; + } + public static void InverseNumMain() + { + int n = int.Parse(Console.ReadLine()); + int[] v = new int[n]; + string s = Console.ReadLine(); + string[] arrStr = s.Split(' '); + for (int i = 0; i < v.Length; i++) + { + v[i] = int.Parse(arrStr[i]); + } + + int[] v_sorted = MergeSort(v, 0, n); + + Console.WriteLine($"{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..987fafe --- /dev/null +++ b/CourseApp/Module2/MergeSort.cs @@ -0,0 +1,62 @@ +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[] left, int[] right) + { + int i = 0, j = 0; + int[] c = new int[left.Length + right.Length]; + for (int k = 0; k < c.Length; k++) + { + if ((j == right.Length) || (i < left.Length && left[i] < right[j])) + { + c[k] = left[i]; + i++; + } + else + { + c[k] = right[j]; + j++; + } + } + + return c; + } + public static int[] mergeSort(int[] num, int l, int r) + { + if ((r - l) == 1) + { + int[] res = new int[1]; + res[0] = num[l]; + return res; + } + int m = (l + r) / 2; + + int[] left = mergeSort(num, l, m); + int[] right = mergeSort(num, m, r); + int[] sort = merge(left, right); + Console.WriteLine($"{l + 1} {r} {sort[0]} {sort[^1]}"); + return sort; + } + public static void MergeSortMain() + { + int n = int.Parse(Console.ReadLine()); + int[] v = new int[n]; + string s = Console.ReadLine(); + string[] arrStr = s.Split(' '); + for (int i = 0; i < v.Length; i++) + { + v[i] = int.Parse(arrStr[i]); + } + int[] v_sorted = mergeSort(v, 0, n); + + Console.WriteLine(string.Join(" ", v_sorted)); + } + } +} diff --git a/CourseApp/Module2/NumOfDiff.cs b/CourseApp/Module2/NumOfDiff.cs new file mode 100644 index 0000000..fa08734 --- /dev/null +++ b/CourseApp/Module2/NumOfDiff.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 NumOfDiff + { + private static long count = 1; + + public static int Partition(int[] arr, int l, int r) + { + int i = l, j = r - 1; + int v = arr[(l + r - 1) / 2]; + while (i <= j) + { + while (arr[i] < v) + { + i++; + } + + while (arr[j] > v) + { + j--; + } + + if (i >= j) + { + break; + } + + (arr[i], arr[j]) = (arr[j], arr[i]); + i++; + j--; + } + return (j + 1); + } + + public static void quickSort(int[] arr, int l, int r) + { + if (r - l <= 1) + { + return; + } + int q = Partition(arr, l, r); + + quickSort(arr, l, q); + quickSort(arr, q, r); + + } + + public static void NumOfDiffMain() + { + int n = int.Parse(Console.ReadLine()); + string str = Console.ReadLine(); + string[] sValues = str.Split(' '); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) + { + arr[i] = int.Parse(sValues[i]); + } + + quickSort(arr, 0, n); + + for (int i = 1; i < n; i++) + { + if (arr[i - 1] != arr[i]) + { + count += 1; + } + } + + Console.WriteLine(count); + } + } +} diff --git a/CourseApp/Module2/SortingPairs.cs b/CourseApp/Module2/SortingPairs.cs new file mode 100644 index 0000000..372cecb --- /dev/null +++ b/CourseApp/Module2/SortingPairs.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + public class SortingPairs + { + public static void SortingPairsMetod() + { + int n = int.Parse(Console.ReadLine()); + int[] arrayID = new int[n]; + int[] arrayPrice = new int[n]; + string[] s = new string[n]; + + for (int i = 0; i < s.Length; i++) + { + s[i] = Console.ReadLine(); + string[] str = s[i].Split(' '); + arrayID[i] = int.Parse(str[0]); + arrayPrice[i] = int.Parse(str[1]); + } + + for (int i = 0; i < s.Length; i++) + { + for (int j = 0; j < s.Length - i - 1; j++) + { + if (arrayPrice[j] < arrayPrice[j + 1]) + { + (arrayPrice[j], arrayPrice[j + 1]) = (arrayPrice[j + 1], arrayPrice[j]); + (arrayID[j], arrayID[j + 1]) = (arrayID[j + 1], arrayID[j]); + } + else if (arrayPrice[j] == arrayPrice[j + 1]) + { + if (arrayID[j] > arrayID[j + 1]) + { + (arrayID[j], arrayID[j + 1]) = (arrayID[j + 1], arrayID[j]); + (arrayPrice[j], arrayPrice[j + 1]) = (arrayPrice[j + 1], arrayPrice[j]); + } + } + } + } + + for (int i = 0; i < s.Length; i++) + { + Console.WriteLine($"{arrayID[i]} {arrayPrice[i]}"); + } + } + } +} diff --git a/CourseApp/Module2/Warehouse.cs b/CourseApp/Module2/Warehouse.cs new file mode 100644 index 0000000..a28891a --- /dev/null +++ b/CourseApp/Module2/Warehouse.cs @@ -0,0 +1,62 @@ +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 WarehouseSort(int[] arrA, int[] arrB, int k) + { + int a = 0; + int b = 0; + int[] c = new int[k + 1]; + int[] arrOrds = new int[k]; + for (int i = 0; i < arrB.Length; i++) + { + c[arrB[i]]++; + } + for (int i = 0; i < c.Length; i++) + { + if (c[i] != 0) + { + arrOrds[a++] = c[i]; + if (arrA[b] >= arrOrds[b]) + { + Console.WriteLine("no"); + } + else + { + Console.WriteLine("yes"); + } + + b++; + } + } + } + private static string[] sValues = null; + public static void WarehouseMain() + { + int a = int.Parse(Console.ReadLine()); + string s = Console.ReadLine(); + sValues = s.Split(' '); + int[] arrA = new int[a]; + for (int i = 0; i < arrA.Length; i++) + { + arrA[i] = int.Parse(sValues[i]); + } + + int b = int.Parse(Console.ReadLine()); + s = Console.ReadLine(); + sValues = s.Split(' '); + int[] arrB = new int[b]; + for (int i = 0; i < arrB.Length; i++) + { + arrB[i] = int.Parse(sValues[i]); + } + WarehouseSort(arrA, arrB, arrA.Length); + } + } +} diff --git a/CourseApp/Module3/CyclicShift.cs b/CourseApp/Module3/CyclicShift.cs new file mode 100644 index 0000000..dff3b98 --- /dev/null +++ b/CourseApp/Module3/CyclicShift.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module3 +{ + public class CyclicShift + { + private static int x = 26; + private static int max = 10000000; + public static int Cyclic(string S, string T) + { + int xt = 1; + int hS = Hash(S); + int hT = Hash(T); + if (hT == hS) return 0; + else if (S.Length == 1) return -1; + else + { + T = S + S; + for (int i = 0; i < S.Length; i++) + { + xt = (xt * x) % max; + } + for (int i = 0; i < S.Length; i++) + { + if (hT == hS) + { + return i; + } + hS = ((x * hS) - (S[i] * xt) + T[i + S.Length]) % max; + hS = (hS + max) % max; + } + } + return -1; + + } + private static int Hash(string str) + { + int hash = 0; + for (int i = 0; i < str.Length; i++) + { + hash = ((hash * x) + str[i]) % max; + } + return hash; + } + + public static void CyclicShift_Main() + { + string S = Console.ReadLine(); + string T = Console.ReadLine(); + S = ReverseMetod(S); + T = ReverseMetod(T); + Console.WriteLine(Cyclic(S, T)); + } + + private static string ReverseMetod(string str) + { + char[] arrStr = str.ToCharArray(); + Array.Reverse(arrStr); + return new string(arrStr); + } + } +} diff --git a/CourseApp/Module3/CyclicString.cs b/CourseApp/Module3/CyclicString.cs new file mode 100644 index 0000000..4e84eb8 --- /dev/null +++ b/CourseApp/Module3/CyclicString.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module3 +{ + public class CyclicString + { + private static int CyclicStringMetod(string str) + { + int[] res = new int[str.Length]; + res[0] = 0; + for (int i = 0; i < str.Length - 1; i++) + { + int j = res[i]; + while (j > 0 && str[i + 1] != str[j]) + { + j = res[j - 1]; + } + if (str[i + 1] == str[j]) res[i + 1] = j + 1; + else res[i + 1] = 0; + } + return str.Length - res[str.Length - 1]; + } + public static void CyclicString_Main() + { + string str = Console.ReadLine(); + + Console.WriteLine(CyclicStringMetod(str)); + } + } +} diff --git a/CourseApp/Module3/StringPeriod.cs b/CourseApp/Module3/StringPeriod.cs new file mode 100644 index 0000000..8951da8 --- /dev/null +++ b/CourseApp/Module3/StringPeriod.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module3 +{ + public class StringPeriod + { + private static int StringPeriodMetod(string str) + { + int count = 0; + int[] arrStr = new int[str.Length]; + for (int i = 0; i < str.Length - 1; i++) + { + int j = arrStr[i]; + while (j > 0 && str[i + 1] != str[j]) + { + j = arrStr[j - 1]; + } + if (str[i + 1] == str[j]) arrStr[i + 1] = j + 1; + else arrStr[i + 1] = 0; + } + count = str.Length - arrStr[str.Length - 1]; + + if (str.Length % count == 0) + { + return str.Length / count; + } + else + { + return 1; + } + } + public static void StringPeriod_Main() + { + string str = Console.ReadLine(); + Console.WriteLine(StringPeriodMetod(str)); + } + } +} diff --git a/CourseApp/Module3/SubstringSearch.cs b/CourseApp/Module3/SubstringSearch.cs new file mode 100644 index 0000000..0265300 --- /dev/null +++ b/CourseApp/Module3/SubstringSearch.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +/*RabinCarp*/ +namespace CourseApp.Module3 +{ + public class SubstringSearch + { + public static ulong CalculateHash(string inp, ulong x, ulong p) + { + ulong hash = 0; + for (int i = 0; i < inp.Length; i++) + { + hash = ((hash * x) + (ulong)(inp[i] - 'A')) % p; + } + + return hash; + } + + public static ulong SlidingHash(string inp, char appended, ulong hash, ulong x, ulong p, ulong multiplier) + { + ulong newHash = hash + p; + newHash -= (multiplier * (ulong)(inp[0] - 'A')) % p; + newHash *= x; + newHash += (ulong)(appended - 'A'); + newHash %= p; + + return newHash; + } + + public static void SubstringSearch_Main() + { + string s = Console.ReadLine(); + string t = Console.ReadLine(); + ulong x = 257; + ulong p = 3571; + + ulong multiplier = 1; + for (int i = 1; i < t.Length; i++) + { + multiplier *= x; + multiplier %= p; + } + + ulong hash_t = 0; + ulong hash_s = 0; + for (int i = 0; i < t.Length; i++) + { + hash_t = ((hash_t * x) + (ulong)(t[i] - 'A')) % p; + hash_s = ((hash_s * x) + (ulong)(s[i] - 'A')) % p; + } + + var res = new List(); + for (int i = 0; i <= (s.Length - t.Length); i++) + { + if (hash_s == hash_t && t == s.Substring(i, t.Length)) + { + res.Add(i); + } + + if ((i + t.Length) < s.Length) + { + hash_s += p; + hash_s -= (multiplier * (ulong)(s[i] - 'A')) % p; + hash_s *= x; + hash_s += (ulong)(s[i + t.Length] - 'A'); + hash_s %= p; + } + } + + string result = string.Join(" ", res); + Console.WriteLine(result); + } + } +} diff --git a/CourseApp/Module4/GetPSP.cs b/CourseApp/Module4/GetPSP.cs new file mode 100644 index 0000000..f9b981a --- /dev/null +++ b/CourseApp/Module4/GetPSP.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class GetPSP + { + private static int getPSPMetod(string str) + { + int openNum = 0, closeNum = 0; + Stack stack = new Stack(str.Length); + for (int i = 0; i < str.Length; i++) + { + stack.Push(str[i]); + } + + for (int i = 0; i < str.Length; i++) + { + if (closeNum == 0 && stack.Peek() == '(') + { + stack.Pop(); + openNum++; + } + else if (closeNum > 0 && stack.Peek() == '(') + { + stack.Pop(); + closeNum--; + } + else if (closeNum <= 0 && stack.Peek() == ')') + { + stack.Pop(); + closeNum++; + } + else + { + stack.Pop(); + closeNum++; + } + } + return openNum + closeNum; + } + public static void GetPSP_Main() + { + string str = Console.ReadLine(); + Console.WriteLine(getPSPMetod(str)); + } + } +} diff --git a/CourseApp/Module4/MinOnSegm.cs b/CourseApp/Module4/MinOnSegm.cs new file mode 100644 index 0000000..6e91b68 --- /dev/null +++ b/CourseApp/Module4/MinOnSegm.cs @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class MinOnSegm + { + private static void MinOnSegmMetod(int n, int k, int[] arr) + { + for (int i = 0; i < k; i++) + { + while (Deque.Empty() == false && arr[i] < arr[Deque.Front()]) + { + Deque.Pop_front(); + } + Deque.Push_front(i); + } + + for (int i = k; i < n; i++) + { + Console.WriteLine(arr[Deque.Back()]); + + while (Deque.Empty() == false && Deque.Back() <= i - k) + { + Deque.Pop_back(); + } + + while (Deque.Empty() == false && arr[i] < arr[Deque.Front()]) + { + Deque.Pop_front(); + if (Deque.Size() == 0) + { + Deque.Clear(); + } + } + Deque.Push_front(i); + } + Console.WriteLine(arr[Deque.Back()]); + } + public static void MinOnSegm_Main() + { + int[] arrNK = Convert(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); + int[] arr = Convert(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); + MinOnSegmMetod(arrNK[0], arrNK[1], arr); + } + private static int[] Convert(string[] arrStr) + { + int[] arr = new int[arrStr.Length]; + for (int i = 0; i < arrStr.Length; i++) + { + arr[i] = int.Parse(arrStr[i]); + } + return arr; + } + } + + public class Deque + { + private static int[] Buffer = new int[100000]; + private static int front = 0; + private static int back = Buffer.Length - 1; + private static int length = 0; + public static void Push_front(int n) + { + front--; + if (front < 0) + { + front = Buffer.Length - 1; + } + Buffer[front] = n; + length++; + } + public static void Push_back(int n) + { + back++; + if (back == Buffer.Length) + { + back = 0; + } + Buffer[back] = n; + length++; + } + public static void Pop_front() + { + front++; + if (front == Buffer.Length) + { + front = 0; + } + length--; + } + + public static void Pop_back() + { + back--; + if (back < 0) + { + back = Buffer.Length - 1; + } + length--; + } + + public static void Clear() + { + front = 0; + back = Buffer.Length - 1; + length = 0; + } + + public static int Front() + { + return Buffer[front]; + } + + public static int Back() + { + return Buffer[back]; + } + + public static int Size() + { + return length; + } + + public static bool Empty() + { + return length == 0; + } + } +} diff --git a/CourseApp/Module4/NearestSmaller.cs b/CourseApp/Module4/NearestSmaller.cs new file mode 100644 index 0000000..d11a6ff --- /dev/null +++ b/CourseApp/Module4/NearestSmaller.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class NearestSmaller + { + private static void NearestSmallerMetod(int n, int[] arr) + { + Stack stack = new Stack(); + int[] outputArr = new int[arr.Length]; + for (int i = outputArr.Length - 1; i >= 0; i--) + { + while (stack.Count() > 0 && arr[stack.Peek()] >= arr[i]) + { + stack.Pop(); + } + if (stack.Count() == 0) + { + outputArr[i] = -1; + } + else + { + outputArr[i] = stack.Peek(); + } + stack.Push(i); + } + for (int i = 0; i < outputArr.Length; i++) + { + Console.Write($"{outputArr[i]} "); + } + } + + public static void NearestSmaller_Main() + { + int n = int.Parse(Console.ReadLine()); + string[] arrStr = Console.ReadLine().Split(' '); + NearestSmallerMetod(n, Convert(arrStr)); + } + private static int[] Convert(string[] arrStr) + { + int[] arr = new int[arrStr.Length]; + for (int i = 0; i < arr.Length; i++) + { + arr[i] = int.Parse(arrStr[i]); + } + return arr; + } + } +} diff --git a/CourseApp/Module4/Sort.cs b/CourseApp/Module4/Sort.cs new file mode 100644 index 0000000..51e0b20 --- /dev/null +++ b/CourseApp/Module4/Sort.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class Sort + { + private static string SortMetod(int n, string[] arr) + { + Stack stack = new Stack(); + int count = 1; + string _output = "YES"; + for (int i = 0; i < n; i++) + { + int num = int.Parse(arr[i]); + + if (stack.Count() > 0 && num > stack.Peek()) + { + _output = "NO"; + break; + } + + stack.Push(num); + while (stack.Count() > 0 && count == stack.Peek()) + { + stack.Pop(); + count++; + } + } + return _output; + } + + public static void Sort_Main() + { + int n = int.Parse(Console.ReadLine()); + string[] arr = Console.ReadLine().Split(' '); + Console.WriteLine(SortMetod(n, arr)); + } + } +} diff --git a/CourseApp/Module4/getPSP.cs b/CourseApp/Module4/getPSP.cs new file mode 100644 index 0000000..f9b981a --- /dev/null +++ b/CourseApp/Module4/getPSP.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module4 +{ + public class GetPSP + { + private static int getPSPMetod(string str) + { + int openNum = 0, closeNum = 0; + Stack stack = new Stack(str.Length); + for (int i = 0; i < str.Length; i++) + { + stack.Push(str[i]); + } + + for (int i = 0; i < str.Length; i++) + { + if (closeNum == 0 && stack.Peek() == '(') + { + stack.Pop(); + openNum++; + } + else if (closeNum > 0 && stack.Peek() == '(') + { + stack.Pop(); + closeNum--; + } + else if (closeNum <= 0 && stack.Peek() == ')') + { + stack.Pop(); + closeNum++; + } + else + { + stack.Pop(); + closeNum++; + } + } + return openNum + closeNum; + } + public static void GetPSP_Main() + { + string str = Console.ReadLine(); + Console.WriteLine(getPSPMetod(str)); + } + } +} diff --git a/CourseApp/Module5/IsTreeBalanced.cs b/CourseApp/Module5/IsTreeBalanced.cs new file mode 100644 index 0000000..6ed0f85 --- /dev/null +++ b/CourseApp/Module5/IsTreeBalanced.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module5 +{ + public class IsTreeBalanced + { + private Node2 root = null; + private List elements; + private static IsTreeBalanced tree = new IsTreeBalanced(); + private bool CheckIsTreeBalanced(Node2 node) + { + if (node == null) + { + return true; + } + + if (Math.Abs(Level(node.left) - Level(node.right)) <= 1 && CheckIsTreeBalanced(node.left) && CheckIsTreeBalanced(node.right)) + { + return true; + } + + return false; + } + + private int Level(Node2 node) + { + if (node == null) + { + return 0; + } + + return 1 + Math.Max(Level(node.left), Level(node.right)); + } + + + private void Insert(int n) + { + root = Inserting(n, root); + } + + private bool Finding(int n, Node2 root) + { + if (root == null) + { + return false; + } + + if (n == root.data) + { + return true; + } + else if (n < root.data) + { + return Finding(n, root.left); + } + else + { + return Finding(n, root.right); + } + } + + private void LastElements(Node2 arr) + { + if (arr == null) + { + return; + } + + LastElements(arr.left); + + if ((arr.left != null && arr.right == null) || (arr.right != null && arr.left == null)) + { + Console.WriteLine(arr.data); + } + + LastElements(arr.right); + } + + private Node2 Inserting(int n, Node2 root) + { + if (root == null) + { + return new Node2(n); + } + + if (root.data > n) + { + root.left = Inserting(n, root.left); + } + else if (root.data < n) + { + root.right = Inserting(n, root.right); + } + + return root; + } + + private void Bypass(Node2 node) + { + if (node == null) + { + return; + } + + Bypass(node.left); + elements.Add(node.data); + Bypass(node.right); + } + + public static void IsTreeBalanced_Main() + { + int[] n = Convert(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); + Console.WriteLine(tree.Check()); + } + private static int[] Convert(string[] arrStr) + { + int[] arr = new int[arrStr.Length - 1]; + for (int i = 0; i < arrStr.Length - 1; i++) + { + tree.Insert(int.Parse(arrStr[i])); + } + + return arr; + } + private string Check() + { + if (CheckIsTreeBalanced(root)) return "YES"; + else return "NO"; + } + } + + public class Node2 + { + public Node2(int n) + { + left = null; + right = null; + data = n; + } + public Node2 left { get; set; } + public Node2 right { get; set; } + public int data { get; set; } + } +} diff --git a/CourseApp/Module5/KSubSection.cs b/CourseApp/Module5/KSubSection.cs new file mode 100644 index 0000000..6084c2c --- /dev/null +++ b/CourseApp/Module5/KSubSection.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module5 +{ + public class KSubSection + { + public static void KSubSection_Main() + { + int length = int.Parse(Console.ReadLine()); + string[] arr = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + int naturalNumber = int.Parse(Console.ReadLine()); + + SubSections tree = new SubSections(arr.Length); + tree.Convert(arr); + + List result = new List(); + + for (int i = 0; i < naturalNumber; i++) + { + string[] arrlrk = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + result.Add(tree.Geti(int.Parse(arrlrk[0]), int.Parse(arrlrk[1]), int.Parse(arrlrk[2]))); + } + + for (int i = 0; i < result.Count; i++) + { + Console.Write(result[i] + " "); + } + } + } + public class SubSections + { + private List[] elements; + + private int size; + + public SubSections(int data) + { + elements = new List[4 * data]; + size = data; + } + public List Check(List first, List second) + { + List list; + + if (first.Count == 0 && second.Count != 0) + { + list = new List(second); + + return list; + } + else if (first.Count != 0 && second.Count != 0) + { + list = new List(first); + list.AddRange(second); + + return list; + } + + list = new List(first); + + return list; + } + public int Geti(int lSide, int rSide, int k) + { + bool trigger = true; + List list = Getingi(0, 0, size, elements, lSide - 1, rSide, ref k, ref trigger); + + if (list.Count < k) + { + return -1; + } + else + { + return list.ElementAt(k - 1); + } + } + private List Getingi(int i, int l, int r, List[] elements, int lSide, int rSide, ref int k, ref bool trigger) + { + if (!trigger) + { + return new List(); + } + + if (lSide <= l && rSide >= r) + { + return elements[i]; + } + + if (lSide >= r || rSide <= l) + { + return new List(); + } + + int mid = (l + r) / 2; + List lResult = Getingi((2 * i) + 1, l, mid, elements, lSide, rSide, ref k, ref trigger); + + if (lResult.Count() != 0) + { + if (lResult.Count() < k) + { + k -= lResult.Count(); + } + else + { + trigger = false; + return lResult; + } + } + + List rResult = Getingi((2 * i) + 2, mid, r, elements, lSide, rSide, ref k, ref trigger); + + if (rResult.Count() != 0) + { + if (rResult.Count() < k) + { + k -= rResult.Count(); + } + else + { + trigger = false; + + return rResult; + } + } + + return new List(); + } + + public void Convert(string[] arrStr) + { + Converting(0, 0, size, arrStr); + } + private void Converting(int i, int l, int r, string[] arrStr) + { + if (r - l == 1) + { + elements[i] = new List(); + if (int.Parse(arrStr[l]) == 0) + { + elements[i].Add(l + 1); + } + + return; + } + + int mid = (l + r) / 2; + + Converting((2 * i) + 1, l, mid, arrStr); + Converting((2 * i) + 2, mid, r, arrStr); + + elements[i] = Check(elements[(2 * i) + 1], elements[(2 * i) + 2]); + } + } +} diff --git a/CourseApp/Module5/KSubSelectionDynamic.cs b/CourseApp/Module5/KSubSelectionDynamic.cs new file mode 100644 index 0000000..d9e55d2 --- /dev/null +++ b/CourseApp/Module5/KSubSelectionDynamic.cs @@ -0,0 +1,222 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module5 +{ + public class KSubSelectionDynamic + { + private static int[] Parse(string[] arrStr) + { + int[] arr = new int[arrStr.Length]; + for (int i = 0; i < arrStr.Length; i++) + { + arr[i] = int.Parse(arrStr[i]); + } + return arr; + } + public static void KSubSelectionDynamic_Main() + { + int length = int.Parse(Console.ReadLine()); + int[] arr = Parse(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); + + int naturalNumber = int.Parse(Console.ReadLine()); + DynamicSubSections tree = new DynamicSubSections(arr.Length); + tree.Convert(arr); + + List result = new List(); + + for (int i = 0; i < naturalNumber; i++) + { + string[] arrlrk = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + if (arrlrk[0] == "u") + { + int first = int.Parse(arrlrk[1]); + int second = int.Parse(arrlrk[2]); + if ((arr[first - 1] != 0 && second == 0) || (arr[first - 1] == 0 && second != 0)) + { + bool add_or_del = (arr[first - 1] != 0 && second == 0) ? true : false; + arr[first - 1] = second; + tree.Update(int.Parse(arrlrk[1]), add_or_del); + } + } + else + result.Add(tree.Geti(int.Parse(arrlrk[1]), int.Parse(arrlrk[2]), int.Parse(arrlrk[3]))); + + } + + for (int i = 0; i < result.Count; i++) + { + Console.Write(result[i] + " "); + } + } + } + public class DynamicSubSections + { + public List[] elements; + + public int size; + + public DynamicSubSections(int data) + { + elements = new List[4 * data]; + size = data; + } + + public void Update(int i, bool trigger) + { + int pos = 0; + Updating(0, 0, size, elements, i - 1, trigger, ref pos); + } + public void Updating(int i, int l, int r, List[] elements, int vdIndex, bool trigger, ref int pos) + { + if (r - l <= 1) + { + if (trigger) + { + elements[i].Add(vdIndex + 1); + } + else + { + elements[i] = new List(); + } + + return; + } + + int mid = (l + r) / 2; + if (vdIndex < mid) + { + Updating((2 * i) + 1, l, mid, elements, vdIndex, trigger, ref pos); + if (trigger) + { + elements[i].Insert(pos, vdIndex + 1); + } + else + { + elements[i].Remove(vdIndex + 1); + } + } + else + { + Updating((2 * i) + 2, mid, r, elements, vdIndex, trigger, ref pos); + if (trigger) + { + if (elements[i].Count() == 0) + { + elements[i].Add(vdIndex + 1); + } + else if (elements[(2 * i) + 2].Count() == 1) + { + pos = elements[i].Count(); + elements[i].Add(vdIndex + 1); + } + else + { + pos = elements[(2 * i) + 1].Count() + pos; + elements[i].Insert(pos, vdIndex + 1); + } + } + else + { + elements[i].Remove(vdIndex + 1); + } + + } + } + + + public int Geti(int lSide, int rSide, int k) + { + bool trigger = true; + List list = Getingi(0, 0, size, elements, lSide - 1, rSide, ref k, ref trigger); + + if (list.Count < k) + { + return -1; + } + else + { + return list.ElementAt(k - 1); + } + } + private List Getingi(int i, int l, int r, List[] elements, int lSide, int rSide, ref int k, ref bool trigger) + { + if (!trigger) + { + return new List(); + } + + if (lSide <= l && rSide >= r) + { + return elements[i]; + } + + if (lSide >= r || rSide <= l) + { + return new List(); + } + + int mid = (l + r) / 2; + List lResult = Getingi((2 * i) + 1, l, mid, elements, lSide, rSide, ref k, ref trigger); + + if (lResult.Count() != 0) + { + if (lResult.Count() < k) + { + k -= lResult.Count(); + } + else + { + trigger = false; + return lResult; + } + } + + List rResult = Getingi((2 * i) + 2, mid, r, elements, lSide, rSide, ref k, ref trigger); + + if (rResult.Count() != 0) + { + if (rResult.Count() < k) + { + k -= rResult.Count(); + } + else + { + trigger = false; + + return rResult; + } + } + + return new List(); + } + + public void Convert(int[] arr) + { + Converting(0, 0, size, arr); + } + private void Converting(int i, int l, int r, int[] arr) + { + if (r - l == 1) + { + elements[i] = new List(); + if (arr[l] == 0) + { + elements[i].Add(l + 1); + } + + return; + } + + int mid = (l + r) / 2; + + Converting((2 * i) + 1, l, mid, arr); + Converting((2 * i) + 2, mid, r, arr); + elements[i] = new List(elements[(2 * i) + 1]); + elements[i].AddRange(elements[(2 * i) + 2]); + } + } +} diff --git a/CourseApp/Module5/NODSubSectionDynamic.cs b/CourseApp/Module5/NODSubSectionDynamic.cs new file mode 100644 index 0000000..b3417db --- /dev/null +++ b/CourseApp/Module5/NODSubSectionDynamic.cs @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module5 +{ + public class NODSubSectionDynamic + { + public static void NODSubSectionDynamic_Main() + { + int length = int.Parse(Console.ReadLine()); + string[] arr = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + int naturalNumber = int.Parse(Console.ReadLine()); + + DynamicSubSections tree = new DynamicSubSections(arr.Length); + tree.Convert(arr); + + List result = new List(); + + for (int i = 0; i < naturalNumber; i++) + { + string[] arrlr = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + if (arrlr[0] == "u") + { + tree.Update(int.Parse(arrlr[1]), int.Parse(arrlr[2])); + } + else + result.Add(tree.GetNOD(int.Parse(arrlr[1]), int.Parse(arrlr[2]))); + } + + for (int i = 0; i < result.Count; i++) + { + Console.Write(result[i] + " "); + } + } + + public class DynamicSubSections + { + private int[] elements; + + private int size; + + public DynamicSubSections(int data) + { + elements = new int[4 * data]; + size = data; + } + + public void Convert(string[] arrStr) + { + Converting(0, 0, size, arrStr); + } + + public int GetNOD(int lSide, int last) + { + return GetNOD(0, 0, size, elements, lSide - 1, last); + } + public void Update(int i, int arr) + { + Updating(0, 0, size, elements, i - 1, arr); + } + private void Updating(int i, int l, int r, int[] elements, int vdIndex, int arr) + { + if (r - l == 1) + { + elements[i] = arr; + return; + } + + int mid = (l + r) / 2; + if (vdIndex < mid) + { + Updating((2 * i) + 1, l, mid, elements, vdIndex, arr); + } + else + { + Updating((2 * i) + 2, mid, r, elements, vdIndex, arr); + } + + elements[i] = NOD(elements[(2 * i) + 1], elements[(2 * i) + 2]); + } + + public int NOD(int a, int b) + { + while (b != 0) + { + int temp = b; + b = a % b; + a = temp; + } + + return a; + } + + private void Converting(int i, int l, int r, string[] arrStr) + { + if (r - l == 1) + { + elements[i] = int.Parse(arrStr[l]); + return; + } + + int mid = (l + r) / 2; + + Converting((2 * i) + 1, l, mid, arrStr); + Converting((2 * i) + 2, mid, r, arrStr); + + elements[i] = NOD(elements[(2 * i) + 1], elements[(2 * i) + 2]); + } + + private int GetNOD(int i, int l, int r, int[] elements, int lSide, int rSide) + { + if (lSide <= l && rSide >= r) + { + return elements[i]; + } + + if (lSide >= r || rSide <= l) + { + return 0; + } + + int mid = (l + r) / 2; + int lResult = GetNOD((2 * i) + 1, l, mid, elements, lSide, rSide); + int rResult = GetNOD((2 * i) + 2, mid, r, elements, lSide, rSide); + + return NOD(lResult, rResult); + } + } + } +} diff --git a/CourseApp/Module5/NODSubSections.cs b/CourseApp/Module5/NODSubSections.cs new file mode 100644 index 0000000..825a00b --- /dev/null +++ b/CourseApp/Module5/NODSubSections.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module5 +{ + public class NODSubSections + { + public static void NODSubSections_Main() + { + int length = int.Parse(Console.ReadLine()); + string[] arr = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + int naturalNumber = int.Parse(Console.ReadLine()); + + SubSections tree = new SubSections(arr.Length); + tree.Convert(arr); + + List result = new List(); + + for (int i = 0; i < naturalNumber; i++) + { + string[] arrlr = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + result.Add(tree.GetNOD(int.Parse(arrlr[0]), int.Parse(arrlr[1]))); + } + + for (int i = 0; i < result.Count; i++) + { + Console.Write(result[i] + " "); + } + } + + public class SubSections + { + private int[] elements; + + private int size; + + public SubSections(int data) + { + elements = new int[4 * data]; + size = data; + } + + public void Convert(string[] arrStr) + { + Converting(0, 0, size, arrStr); + } + + public int GetNOD(int lSide, int last) + { + return GetNOD(0, 0, size, elements, lSide - 1, last); + } + + public int NOD(int a, int b) + { + while (b != 0) + { + int temp = b; + b = a % b; + a = temp; + } + + return a; + } + + private void Converting(int i, int l, int r, string[] arrStr) + { + if (r - l == 1) + { + elements[i] = int.Parse(arrStr[l]); + return; + } + + int mid = (l + r) / 2; + + Converting((2 * i) + 1, l, mid, arrStr); + Converting((2 * i) + 2, mid, r, arrStr); + + elements[i] = NOD(elements[(2 * i) + 1], elements[(2 * i) + 2]); + } + + private int GetNOD(int i, int l, int r, int[] elements, int lSide, int rSide) + { + if (lSide <= l && rSide >= r) + { + return elements[i]; + } + + if (lSide >= r || rSide <= l) + { + return 0; + } + + int mid = (l + r) / 2; + int lResult = GetNOD((2 * i) + 1, l, mid, elements, lSide, rSide); + int rResult = GetNOD((2 * i) + 2, mid, r, elements, lSide, rSide); + + return NOD(lResult, rResult); + } + } + } +} diff --git a/CourseApp/Module5/OutBinaryTree.cs b/CourseApp/Module5/OutBinaryTree.cs new file mode 100644 index 0000000..1347e34 --- /dev/null +++ b/CourseApp/Module5/OutBinaryTree.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module5 +{ + public class OutBinaryTree + { + private Node root = null; + private List elements; + private static OutBinaryTree tree = new OutBinaryTree(); + + private void Insert(int n) + { + root = Inserting(n, root); + } + private bool Finding(int n, Node root) + { + if (root == null) + { + return false; + } + + if (n == root.data) + { + return true; + } + else if (n < root.data) + { + return Finding(n, root.left); + } + else + { + return Finding(n, root.right); + } + } + + private void FindLastElements() + { + LastElements(root); + } + + private void LastElements(Node arr) + { + if (arr == null) + { + return; + } + + LastElements(arr.left); + + if ((arr.left != null && arr.right == null) || (arr.right != null && arr.left == null)) + { + Console.WriteLine(arr.data); + } + + LastElements(arr.right); + } + + private Node Inserting(int n, Node root) + { + if (root == null) + { + return new Node(n); + } + + if (root.data > n) + { + root.left = Inserting(n, root.left); + } + else if (root.data < n) + { + root.right = Inserting(n, root.right); + } + + return root; + } + + private void Bypass(Node node) + { + if (node == null) + { + return; + } + + Bypass(node.left); + elements.Add(node.data); + Bypass(node.right); + } + + public static void OutBinaryTree_Main() + { + int[] n = Convert(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); + } + private static int[] Convert(string[] arrStr) + { + int[] arr = new int[arrStr.Length - 1]; + for (int i = 0; i < arrStr.Length - 1; i++) + { + tree.Insert(int.Parse(arrStr[i])); + } + tree.FindLastElements(); + return arr; + } + } + + public class Node + { + public Node(int n) + { + left = null; + right = null; + data = n; + } + public Node left { get; set; } + public Node right { get; set; } + public int data { get; set; } + } +} diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..989e0fd 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -7,9 +7,8 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - - Console.WriteLine("Hello World"); - } + // BubbleSort.BubbleSortMethod(); + // SortingPairs.SortingPairsMetod(); + } } } diff --git a/README.md b/README.md index e58af8a..a7ac121 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ -# Tprogramming_42_2020 +# Tprogramming_147_2022 +Kulichkov Oleg Master branch :) \ No newline at end of file