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..4e39e7c 100644 --- a/CourseApp.Tests/Module2/BubbleSortTest.cs +++ b/CourseApp.Tests/Module2/BubbleSortTest.cs @@ -1,18 +1,22 @@ -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 = @"3 --10 7 2"; + 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"; public void Dispose() { @@ -24,8 +28,7 @@ public void Dispose() } [Theory] - [InlineData(Inp1, "1 1 3 4 5 7 9")] - [InlineData(Inp2, "-10 2 7")] + [InlineData(Inp1, Out1)] public void Test1(string input, string expected) { var stringWriter = new StringWriter(); @@ -39,7 +42,9 @@ public void Test1(string input, string expected) // assert var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - Assert.Equal($"{expected}", output[0]); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); } } -} +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/BubleTest/BubleTest.sln b/CourseApp.Tests/Module2/BubleTest/BubleTest.sln new file mode 100644 index 0000000..56e09c3 --- /dev/null +++ b/CourseApp.Tests/Module2/BubleTest/BubleTest.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32210.238 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BubleTest", "BubleTest\BubleTest.csproj", "{B4D38834-5CE0-40AE-8E5F-FB02E3015602}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B4D38834-5CE0-40AE-8E5F-FB02E3015602}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B4D38834-5CE0-40AE-8E5F-FB02E3015602}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B4D38834-5CE0-40AE-8E5F-FB02E3015602}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B4D38834-5CE0-40AE-8E5F-FB02E3015602}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F9C25243-48C8-4F6D-936E-F81DE436D4BC} + EndGlobalSection +EndGlobal diff --git a/CourseApp.Tests/Module2/BubleTest/BubleTest/BubleTest.csproj b/CourseApp.Tests/Module2/BubleTest/BubleTest/BubleTest.csproj new file mode 100644 index 0000000..f72bee8 --- /dev/null +++ b/CourseApp.Tests/Module2/BubleTest/BubleTest/BubleTest.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + + false + + + + + + + + + + diff --git a/CourseApp.Tests/Module2/BubleTest/BubleTest/UnitTest1.cs b/CourseApp.Tests/Module2/BubleTest/BubleTest/UnitTest1.cs new file mode 100644 index 0000000..3750d39 --- /dev/null +++ b/CourseApp.Tests/Module2/BubleTest/BubleTest/UnitTest1.cs @@ -0,0 +1,13 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BubleTest +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public void TestMethod1() + { + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/InversionCountTest.cs b/CourseApp.Tests/Module2/InversionCountTest.cs new file mode 100644 index 0000000..c10924d --- /dev/null +++ b/CourseApp.Tests/Module2/InversionCountTest.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 InversionCountTest : IDisposable + { + private const string Inp1 = @"1 +1"; + + private const string Out1 = @"0"; + + private const string Inp2 = @"5 +5 4 3 2 1"; + + private const string Out2 = @"10"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(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 + InversionCount.Start(); + + // 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/MergeTest.cs b/CourseApp.Tests/Module2/MergeTest.cs new file mode 100644 index 0000000..844bff1 --- /dev/null +++ b/CourseApp.Tests/Module2/MergeTest.cs @@ -0,0 +1,63 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class MergeSortTest : IDisposable + { + private const string Inp1 = @"5 +5 4 3 2 1"; + + private const string Out1 = @"1 2 4 5 +4 5 1 2 +3 5 1 3 +1 5 1 5 +1 2 3 4 5"; + + private const string Inp2 = @"1 +1"; + + private const string Out2 = @"1"; + + private const string Inp3 = @"2 +3 1"; + + private const string Out3 = @"1 2 1 3 +1 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)] + [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.MergeSortMethod(); + + // 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/NumberDifTest.cs b/CourseApp.Tests/Module2/NumberDifTest.cs new file mode 100644 index 0000000..446ed42 --- /dev/null +++ b/CourseApp.Tests/Module2/NumberDifTest.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 NumberDifTest : 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 + NumberDif.Count_Diff_Method(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/PairSortTest.cs b/CourseApp.Tests/Module2/PairSortTest.cs new file mode 100644 index 0000000..c7fea56 --- /dev/null +++ b/CourseApp.Tests/Module2/PairSortTest.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 PairSortTest : IDisposable + { + private const string Inp1 = @"3 +20 80 +30 90 +25 90"; + + private const string Out1 = @"25 90 +30 90 +20 80"; + + private const string Inp2 = @"3 +101 80 +305 90 +200 14"; + + private const string Out2 = @"305 90 +101 80 +200 14"; + + 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 + PairSort.PairSortMethod(); + + // 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/RadixSortTest.cs b/CourseApp.Tests/Module2/RadixSortTest.cs new file mode 100644 index 0000000..2cfec28 --- /dev/null +++ b/CourseApp.Tests/Module2/RadixSortTest.cs @@ -0,0 +1,82 @@ +namespace CourseApp.Tests.Module2 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module2; + using Xunit; + + [Collection("Sequential")] + public class RadixSortTest : IDisposable + { + private const string Inp1 = @"9 +12 +32 +45 +67 +98 +29 +61 +35 +09"; + + private const string Out1 = @"Initial array: +12, 32, 45, 67, 98, 29, 61, 35, 09 +** +Phase 1 +Bucket 0: empty +Bucket 1: 61 +Bucket 2: 12, 32 +Bucket 3: empty +Bucket 4: empty +Bucket 5: 45, 35 +Bucket 6: empty +Bucket 7: 67 +Bucket 8: 98 +Bucket 9: 29, 09 +** +Phase 2 +Bucket 0: 09 +Bucket 1: 12 +Bucket 2: 29 +Bucket 3: 32, 35 +Bucket 4: 45 +Bucket 5: empty +Bucket 6: 61, 67 +Bucket 7: empty +Bucket 8: empty +Bucket 9: 98 +** +Sorted array: +09, 12, 29, 32, 35, 45, 61, 67, 98"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + RadixSort.Start(); + + // 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..5302551 --- /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.Input_Values(); + + // 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/CircularStrTest.cs b/CourseApp.Tests/Module3/CircularStrTest.cs new file mode 100644 index 0000000..7fb74cb --- /dev/null +++ b/CourseApp.Tests/Module3/CircularStrTest.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class CircularStrTest : 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 + CircularStr.EnterVal(); + + // 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/CyclingShiftTest.cs b/CourseApp.Tests/Module3/CyclingShiftTest.cs new file mode 100644 index 0000000..09a7b3f --- /dev/null +++ b/CourseApp.Tests/Module3/CyclingShiftTest.cs @@ -0,0 +1,51 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class CyclingShiftTest : IDisposable + { + private const string Inp1 = @"a + b"; + + private const string Out1 = @"-1"; + + private const string Inp2 = @"zabcd +abcdz"; + + private const string Out2 = @"4"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + CyclicShift.EnterValues(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module3/FindSubstrTest.cs b/CourseApp.Tests/Module3/FindSubstrTest.cs new file mode 100644 index 0000000..a9a199d --- /dev/null +++ b/CourseApp.Tests/Module3/FindSubstrTest.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Tests.Module3 +{ + internal class Class1 + { + } +} diff --git a/CourseApp.Tests/Module3/PeriodStrTest.cs b/CourseApp.Tests/Module3/PeriodStrTest.cs new file mode 100644 index 0000000..9d0a393 --- /dev/null +++ b/CourseApp.Tests/Module3/PeriodStrTest.cs @@ -0,0 +1,54 @@ +namespace CourseApp.Tests.Module3 +{ + using System; + using System.IO; + using CourseApp.Module3; + using Xunit; + + [Collection("Sequential")] + public class PeriodStrTest : 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 + PeriodStr.EnterVal(); + + // 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/MinSegTest.cs b/CourseApp.Tests/Module4/MinSegTest.cs new file mode 100644 index 0000000..84ae485 --- /dev/null +++ b/CourseApp.Tests/Module4/MinSegTest.cs @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class MinSegTest : 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 + MinSeg.GetMinSeg(); + + // 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/NearSmalTest.cs b/CourseApp.Tests/Module4/NearSmalTest.cs new file mode 100644 index 0000000..681fbfe --- /dev/null +++ b/CourseApp.Tests/Module4/NearSmalTest.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class NearSmalTest : 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 + NearSmal.GetNearestSmaller(); + + // 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/PspTest.cs b/CourseApp.Tests/Module4/PspTest.cs new file mode 100644 index 0000000..7053e67 --- /dev/null +++ b/CourseApp.Tests/Module4/PspTest.cs @@ -0,0 +1,49 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class PspTest : IDisposable + { + private const string Inp1 = @"())(()"; + + private const string Out1 = @"2"; + + private const string Inp2 = @"))((("; + + private const string Out2 = @"5"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + Psp.GetPsp(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module4/SortTest.cs b/CourseApp.Tests/Module4/SortTest.cs new file mode 100644 index 0000000..62fddb8 --- /dev/null +++ b/CourseApp.Tests/Module4/SortTest.cs @@ -0,0 +1,58 @@ +namespace CourseApp.Tests.Module4 +{ + using System; + using System.Collections.Generic; + using System.IO; + using CourseApp.Module4; + using Xunit; + + [Collection("Sequential")] + public class SortTest : IDisposable + { + private const string Inp1 = @"3 +3 2 1"; + + private const string Out1 = @"YES"; + + private const string Inp2 = @"4 +4 1 3 2"; + + private const string Out2 = @"YES"; + + private const string Inp3 = @"3 +2 3 1"; + + private const string Out3 = @"NO"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + [InlineData(Inp3, Out3)] + public void Test1(string input, string expected) + { + var stringWriter = new StringWriter(); + Console.SetOut(stringWriter); + + var stringReader = new StringReader(input); + Console.SetIn(stringReader); + + // act + SORT.GetSorting(); + + // 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/BinarTreeBranchTest.cs b/CourseApp.Tests/Module5/BinarTreeBranchTest.cs new file mode 100644 index 0000000..9edc12e --- /dev/null +++ b/CourseApp.Tests/Module5/BinarTreeBranchTest.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.IO; + using CourseApp.Module5; + using Xunit; + + [Collection("Sequential")] + public class BinarTreeBranchTest : 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 + BinarTreeBranch.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.Tests/Module5/IsTreeBalancedTest.cs b/CourseApp.Tests/Module5/IsTreeBalancedTest.cs new file mode 100644 index 0000000..7acc536 --- /dev/null +++ b/CourseApp.Tests/Module5/IsTreeBalancedTest.cs @@ -0,0 +1,45 @@ +namespace CourseApp.Tests.Module5 +{ + using System; + using System.IO; + using CourseApp.Module5; + using CourseApp.Module5.Task_1; + 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 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 + IsTreeBalanced.IsTreeBalancedMethod(); + + // assert + var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + var result = string.Join(Environment.NewLine, output); + + Assert.Equal($"{expected}", result); + } + } +} \ No newline at end of file diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index 9551450..5b415d8 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp2.1 + netcoreapp6 True 1573,1591,1701;1702;1705; diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..5343e8c 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -4,35 +4,42 @@ namespace CourseApp.Module2 { - public class BubbleSort + public class Bubble_Sort { - public static void BubbleSortMethod() + public static void Bubble_Sort_Method() { int n = int.Parse(Console.ReadLine()); string s = Console.ReadLine(); string[] sValues = s.Split(' '); - int[] arr = new int[n]; + int[] array = new int[n]; for (int i = 0; i < n; i++) { - arr[i] = int.Parse(sValues[i]); + array[i] = int.Parse(sValues[i]); } - for (int i = 0; i < arr.Length - 1; i++) + bool swap = false; + for (int i = 0; i < array.Length - 1; i++) { - for (int j = 0; j < arr.Length - i - 1; j++) + for (int j = 0; j < array.Length - i - 1; j++) { - if (arr[j] > arr[j + 1]) + if (array[j] > array[j + 1]) { - // int temp = arr[j]; - // arr[j] = arr[j + 1]; - // arr[j+1] = temp; - (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]); + (array[j], array[j + 1]) = (array[j + 1], array[j]); + string result = string.Join(" ", array); + Console.WriteLine(result); + swap = true; } } } - string result = string.Join(" ", arr); - Console.WriteLine(result); + if (swap == false) + { + Console.WriteLine(0); + } + } + public static void Main() + { + Bubble_Sort_Method(); } } -} +} \ No newline at end of file diff --git a/CourseApp/Module2/BubleSort/BubleSort.sln b/CourseApp/Module2/BubleSort/BubleSort.sln new file mode 100644 index 0000000..68c9fbe --- /dev/null +++ b/CourseApp/Module2/BubleSort/BubleSort.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32210.238 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BubleSort", "BubleSort\BubleSort.csproj", "{FCC83734-13D9-481B-B5AE-365A3D2E054A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FCC83734-13D9-481B-B5AE-365A3D2E054A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FCC83734-13D9-481B-B5AE-365A3D2E054A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FCC83734-13D9-481B-B5AE-365A3D2E054A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FCC83734-13D9-481B-B5AE-365A3D2E054A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {98B7A04B-F6B3-462E-8496-40EA4DE95C8B} + EndGlobalSection +EndGlobal diff --git a/CourseApp/Module2/BubleSort/BubleSort/BubleSort.csproj b/CourseApp/Module2/BubleSort/BubleSort/BubleSort.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/CourseApp/Module2/BubleSort/BubleSort/BubleSort.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/CourseApp/Module2/BubleSort/BubleSort/Program.cs b/CourseApp/Module2/BubleSort/BubleSort/Program.cs new file mode 100644 index 0000000..4c041ad --- /dev/null +++ b/CourseApp/Module2/BubleSort/BubleSort/Program.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CourseApp.Module2 +{ + 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++) + { + arr[i] = int.Parse(sValues[i]); + } + + for (int i = 0; i < arr.Length - 1; i++) + { + for (int j = 0; j < arr.Length - i - 1; j++) + { + if (arr[j] > arr[j + 1]) + { + // int temp = arr[j]; + // arr[j] = arr[j + 1]; + // arr[j+1] = temp; + (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]); + } + } + } + + string result = string.Join(" ", arr); + Console.WriteLine(result); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/InversionCount.cs b/CourseApp/Module2/InversionCount.cs new file mode 100644 index 0000000..25ed435 --- /dev/null +++ b/CourseApp/Module2/InversionCount.cs @@ -0,0 +1,82 @@ +using System; + +namespace CourseApp.Module2 +{ + public class InversionCount + { + private static long inversionCount = 0; + + public static void Start() + { + int v = int.Parse(Console.ReadLine()); + if (v > 1) + { + string temp = Console.ReadLine(); + string[] values = temp.Split(' '); + int[] array = new int[v]; + for (int i = 0; i < values.Length; i++) + { + array[i] = int.Parse(values[i]); + } + + int[] result = Sort(array, 0, v); + Console.WriteLine(inversionCount); + } + else + { + Console.WriteLine(0); + } + } + + public static int[] Sort(int[] array, int firstInd, int lastInd) + { + if (lastInd - firstInd == 1) + { + int[] res = new int[1]; + res[0] = array[firstInd]; + return res; + } + + int w = (firstInd + lastInd) / 2; + + int[] left = Sort(array, firstInd, w); + int[] right = Sort(array, w, lastInd); + + return Merge(left, right); + } + + public static int[] Merge(int[] left, int[] right) + { + int i = 0; + int j = 0; + int[] result = new int[left.Length + right.Length]; + + for (int n = 0; n < result.Length; n++) + { + if (i == left.Length) + { + result[n] = right[j]; + j++; + } + else if (j == right.Length) + { + result[n] = left[i]; + i++; + } + else if (left[i] <= right[j]) + { + result[n] = left[i]; + i++; + } + else + { + result[n] = right[j]; + j++; + inversionCount += left.Length - i; + } + } + + return result; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/Merge Sort.cs b/CourseApp/Module2/Merge Sort.cs new file mode 100644 index 0000000..03b6407 --- /dev/null +++ b/CourseApp/Module2/Merge Sort.cs @@ -0,0 +1,82 @@ +using System; + +namespace CourseApp.Module2 +{ + public class MergeSort + { + public static void Main() + { + MergeSortMethod(); + } + public static void MergeSortMethod() + { + int[] arr = InputParse(); + + int[] sortedArr = ArrSort(ref arr, 0, arr.Length); + + Console.WriteLine("{0}", string.Join(" ", sortedArr)); + } + + private static int[] Merge(ref int[] left, ref int[] right) + { + int i = 0; + int j = 0; + int[] add = new int[left.Length + right.Length]; + for (int k = 0; k < add.Length; k++) + { + if (i == left.Length) + { + add[k] = right[j]; + j++; + } + else if (j == right.Length || left[i] <= right[j]) + { + add[k] = left[i]; + i++; + } + else + { + add[k] = right[j]; + j++; + } + } + + return add; + } + + private static int[] ArrSort(ref int[] arr, int begin, int end) + { + if (end - begin == 1) + { + int[] res = new int[1]; + res[0] = arr[begin]; + return res; + } + + int mid = (begin + end) / 2; + + int[] left = ArrSort(ref arr, begin, mid); + int[] right = ArrSort(ref arr, mid, end); + + int[] sort = Merge(ref left, ref right); + + Console.WriteLine("{0} {1} {2} {3}", begin + 1, end, sort[0], sort[^1]); + + return Merge(ref left, ref right); + } + + private static int[] InputParse() + { + int n = int.Parse(Console.ReadLine()); + int[] arr = new int[n]; + string s = Console.ReadLine(); + string[] sValues = s.Split(' '); + for (int i = 0; i < n; i++) + { + arr[i] = int.Parse(sValues[i]); + } + + return arr; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/NumberDif.cs b/CourseApp/Module2/NumberDif.cs new file mode 100644 index 0000000..c2c1f69 --- /dev/null +++ b/CourseApp/Module2/NumberDif.cs @@ -0,0 +1,77 @@ +using System; + +namespace CourseApp.Module2 +{ + public class NumberDif + { + public static void Count_Diff_Method() + { + int number = int.Parse(Console.ReadLine()); + string[] value = Console.ReadLine().Split(' '); + int[] array = new int[number]; + + for (int i = 0; i < number; ++i) + { + array[i] = int.Parse(value[i]); + } + + QuickSort(array, 0, number); + + int count = 0; + + for (int i = 1; i < number; i++) + { + if (array[i] != array[i - 1]) + { + count++; + } + } + + Console.Write(count + 1); + } + + public static int Partition(int[] array, int leftInd, int rightInd) + { + int i = leftInd; + int j = rightInd - 1; + int w = array[(leftInd + rightInd - 1) / 2]; + + while (i <= j) + { + while (array[i] < w) + { + i++; + } + + while (array[j] > w) + { + j--; + } + + if (i >= j) + { + break; + } + + (array[i], array[j]) = (array[j], array[i]); + i++; + j--; + } + + return j + 1; + } + + public static void QuickSort(int[] array, int leftInd, int rightInd) + { + if (rightInd - leftInd <= 1) + { + return; + } + + int v = Partition(array, leftInd, rightInd); + + QuickSort(array, leftInd, v); + QuickSort(array, v, rightInd); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/Pair.cs b/CourseApp/Module2/Pair.cs new file mode 100644 index 0000000..e56bebd --- /dev/null +++ b/CourseApp/Module2/Pair.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module2 +{ + public class PairSort + { + public static void Main() + { + PairSortMethod(); + } + public static void PairSortMethod() + { + int[,] arr = InputParse(); + + int n = arr.Length / 2; + + SortArrFirst(ref arr, ref n); + SortArrSecond(ref arr, ref n); + Show(ref arr, ref n); + } + + private static void SortArrFirst(ref int[,] arr, ref int n) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n - i - 1; j++) + { + if (arr[j + 1, 1] > arr[j, 1]) + { + Swap(ref arr[j, 0], ref arr[j + 1, 0]); + Swap(ref arr[j, 1], ref arr[j + 1, 1]); + } + } + } + } + + private static void SortArrSecond(ref int[,] arr, ref int n) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n - i - 1; j++) + { + if (arr[j + 1, 1] == arr[j, 1] && arr[j + 1, 0] < arr[j, 0]) + { + Swap(ref arr[j, 0], ref arr[j + 1, 0]); + Swap(ref arr[j, 1], ref arr[j + 1, 1]); + } + } + } + } + + private static int[,] InputParse() + { + 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(' '); + for (int j = 0; j < 2; j++) + { + arr[i, j] = int.Parse(sValues[j]); + } + } + + return arr; + } + + private static void Show(ref int[,] arr, ref int n) + { + string result = null; + for (int i = 0; i < n; i++) + { + result = arr[i, 0] + " " + arr[i, 1]; + Console.WriteLine(result); + } + } + + private static void Swap(ref int left, ref int right) + { + int temp = right; + right = left; + left = temp; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/RadixSort.cs b/CourseApp/Module2/RadixSort.cs new file mode 100644 index 0000000..1f82146 --- /dev/null +++ b/CourseApp/Module2/RadixSort.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 RadixSort + { + public static void RadSort(string[] arr_string) + { + int numb_phaze = 1; + int rank = arr_string[0].Length; + + Console.WriteLine("Initial array:"); + Console.WriteLine("{0}", string.Join(", ", arr_string)); + + foreach (var i in Enumerable.Range(0, Convert.ToInt32(Math.Ceiling(Convert.ToDouble(-1 - (rank - 1)) / -1))).Select(x_1 => rank - 1 + (x_1 * -1))) + { + Console.WriteLine("**********"); + Console.WriteLine("Phase {0}", numb_phaze); + ulong n; + List[] arrayList = new List[10]; + for (n = 0; n < 10; n++) + { + arrayList[n] = new List(); + } + + for (int j = 0; j < arr_string.Length; j++) + { + int k = int.Parse(arr_string[j].Substring(rank - numb_phaze, 1)); + arrayList[k].Add(arr_string[j]); + } + + for (n = 0; n < 10; n++) + { + if (arrayList[n].Count == 0) + { + Console.WriteLine("Bucket " + n + ": empty"); + } + else + { + Console.WriteLine("Bucket " + n + ": {0}", string.Join(", ", arrayList[n])); + } + } + + int l = 0; + + for (n = 0; n < 10; n++) + { + for (int j = 0; j < arrayList[n].Count; j++) + { + arr_string[l] = arrayList[n][j]; + l++; + } + } + + numb_phaze++; + } + + Console.WriteLine("**********"); + Console.WriteLine("Sorted array:"); + Console.Write("{0}", string.Join(", ", arr_string)); + } + + public static void Start() + { + ulong m = ulong.Parse(Console.ReadLine()); + string[] arr_string = new string[m]; + for (ulong i = 0; i < m; i++) + { + arr_string[i] = Console.ReadLine(); + } + + RadSort(arr_string); + } + } +} diff --git a/CourseApp/Module2/Warehouse.cs b/CourseApp/Module2/Warehouse.cs new file mode 100644 index 0000000..f8e8167 --- /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 Count_Sort(int[] orders_arr, int[] products_arr, int n) + { + int[] arr = new int[n + 1]; + for (int i = 0; i < orders_arr.Length; i++) + { + arr[orders_arr[i]]++; + } + + int pos = 0; + int index = 0; + int[] ord = new int[n]; + string[] answer = new string[n]; + for (int i = 0; i < arr.Length; i++) + { + if (arr[i] != 0) + { + ord[pos++] = arr[i]; + if (products_arr[index] >= ord[index]) + { + Console.WriteLine("no"); + } + else + { + Console.WriteLine("yes"); + } + + index++; + } + } + } + + public static void Input_Values() + { + int products_len = int.Parse(Console.ReadLine()); + string products_string = Console.ReadLine(); + string[] parseValues = products_string.Split(' '); + int[] products_arr = new int[products_len]; + for (int i = 0; i < products_len; i++) + { + products_arr[i] = int.Parse(parseValues[i]); + } + + int orders_len = int.Parse(Console.ReadLine()); + string orders_string = Console.ReadLine(); + parseValues = orders_string.Split(' '); + int[] orders_arr = new int[orders_len]; + for (int i = 0; i < orders_len; i++) + { + orders_arr[i] = int.Parse(parseValues[i]); + } + + Count_Sort(orders_arr, products_arr, products_len); + } + } +} diff --git a/CourseApp/Module3/CircularStr.cs b/CourseApp/Module3/CircularStr.cs new file mode 100644 index 0000000..4621834 --- /dev/null +++ b/CourseApp/Module3/CircularStr.cs @@ -0,0 +1,45 @@ +using System; + +namespace CourseApp.Module3 +{ + public class CircularStr + { + public static int[] Prefix_function(string b) + { + int[] res = new int[b.Length]; + res[0] = 0; + + for (int i = 0; i < b.Length - 1; i++) + { + int j = res[i]; + + while (j > 0 && b[i + 1] != b[j]) + { + j = res[j - 1]; + } + + if (b[i + 1] == b[j]) + { + res[i + 1] = j + 1; + } + else + { + res[i + 1] = 0; + } + } + + return res; + } + + public static void EnterVal() + { + string b = Console.ReadLine(); + + int[] prefixs = Prefix_function(b); + + int result = b.Length - prefixs[b.Length - 1]; + + Console.WriteLine(result); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/CyclicShift.cs b/CourseApp/Module3/CyclicShift.cs new file mode 100644 index 0000000..07a480d --- /dev/null +++ b/CourseApp/Module3/CyclicShift.cs @@ -0,0 +1,74 @@ +using System; +using System.Linq; + +namespace CourseApp.Module3 +{ + public class CyclicShift + { + public static int Rabin_Karp(string v, string n) + { + if (v == n) + { + return 0; + } + + n = string.Concat(Enumerable.Repeat(n, 2)); + + long w = 13; + long b = 1; + long t = 100000000; + + long first_hash = 0; + long second_hash = 0; + long xt = 1; + + foreach (char i in v.Reverse()) + { + first_hash = (first_hash + (i * b)) % t; + b = (b * w) % t; + } + + b = 1; + + for (int i = v.Length - 1; i >= 0; i--) + { + second_hash = (second_hash + (n[i] * b)) % t; + b = (b * w) % t; + } + + for (int i = 0; i < v.Length - 1; i++) + { + xt = (xt * w) % t; + } + + for (int i = 1; i < n.Length - v.Length + 1; i++) + { + if (first_hash == second_hash) + { + return i - 1; + } + + second_hash = w * (second_hash - (n[i - 1] * xt)); + second_hash += n[i + v.Length - 1]; + second_hash = second_hash % t; + + if ((second_hash < 0 && t > 0) || (second_hash > 0 && t < 0)) + { + second_hash += t; + } + } + + return -1; + } + + public static void EnterValues() + { + string r = Console.ReadLine(); + string q = Console.ReadLine(); + + int result = Rabin_Karp(r, q); + + Console.WriteLine(result); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/FindSubstr.cs b/CourseApp/Module3/FindSubstr.cs new file mode 100644 index 0000000..b5f3464 --- /dev/null +++ b/CourseApp/Module3/FindSubstr.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CourseApp.Module3 +{ + public class FindSubstr + { + public static long Get_hash(string w, int b, int q, int t) + { + long res = 0; + for (int i = 0; i < b; i++) + { + res = ((res * t) + w[i]) % q; + } + + return res; + } + + public static void Rabin_karp(string e, string k, int o, int v) + { + long ht = Get_hash(k, k.Length, o, v); + + long hs = Get_hash(e, k.Length, o, v); + + long xt = 1; + + for (int i = 0; i < k.Length; i++) + { + xt = (xt * v) % o; + } + + for (int i = 0; i <= e.Length - k.Length; i++) + { + if (ht == hs) + { + Console.Write("{0} ", i); + } + + if (i + k.Length < e.Length) + { + hs = ((hs * v) - (e[i] * xt) + e[i + k.Length]) % o; + hs = (hs + o) % o; + } + } + } + + public static void EnterValues() + { + string e = Console.ReadLine(); + string k = Console.ReadLine(); + + int o = 67953405; + int v = 26; + + Rabin_karp(e, k, o, v); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/PeriodStr.cs b/CourseApp/Module3/PeriodStr.cs new file mode 100644 index 0000000..826023d --- /dev/null +++ b/CourseApp/Module3/PeriodStr.cs @@ -0,0 +1,52 @@ +using System; + +namespace CourseApp.Module3 +{ + public class PeriodStr + { + public static int[] PrefixFunc(string k) + { + int[] res = new int[k.Length]; + res[0] = 0; + + for (int i = 0; i < k.Length - 1; i++) + { + int j = res[i]; + + while (j > 0 && k[i + 1] != k[j]) + { + j = res[j - 1]; + } + + if (k[i + 1] == k[j]) + { + res[i + 1] = j + 1; + } + else + { + res[i + 1] = 0; + } + } + + return res; + } + + public static void EnterVal() + { + string k = Console.ReadLine(); + + int[] prefixs = PrefixFunc(k); + + int result = k.Length - prefixs[k.Length - 1]; + + if (k.Length % result == 0) + { + Console.WriteLine(k.Length / result); + } + else + { + Console.WriteLine(1); + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/MinSeg.cs b/CourseApp/Module4/MinSeg.cs new file mode 100644 index 0000000..ba9b01e --- /dev/null +++ b/CourseApp/Module4/MinSeg.cs @@ -0,0 +1,137 @@ +using System; + +public class MinSeg +{ + public static void GetMinSeg() + { + string sFirst = Console.ReadLine(); + string[] sFirstVal = sFirst.Split(' '); + int[] arrayFirst = new int[2]; + for (int i = 0; i < 2; i++) + { + arrayFirst[i] = int.Parse(sFirstVal[i]); + } + + string sSecond = Console.ReadLine(); + string[] sSecondVal = sSecond.Split(' '); + int[] arraySecond = new int[arrayFirst[0]]; + for (int i = 0; i < arrayFirst[0]; i++) + { + arraySecond[i] = int.Parse(sSecondVal[i]); + } + + for (int i = 0; i < arrayFirst[1]; i++) + { + while (Deque.Empty() == false && arraySecond[i] < arraySecond[Deque.Front()]) + { + Deque.Pop_Front(); + } + + Deque.Push_Front(i); + } + + for (int i = arrayFirst[1]; i < arrayFirst[0]; i++) + { + Console.WriteLine(arraySecond[Deque.Back()]); + + while (Deque.Empty() == false && Deque.Back() <= i - arrayFirst[1]) + { + Deque.Pop_Back(); + } + + while (Deque.Empty() == false && arraySecond[i] < arraySecond[Deque.Front()]) + { + Deque.Pop_Front(); + if (Deque.Size() == 0) + { + Deque.Clear(); + } + } + + Deque.Push_Front(i); + } + + Console.WriteLine(arraySecond[Deque.Back()]); + } + + private class Deque + { + private static int[] buffer = new int[100000001]; + private static int front = 0; + private static int back = 100000001 - 1; + private static int size = 0; + + public static void Push_Back(int t) + { + back++; + if (back == 100000001) + { + back = 0; + } + + buffer[back] = t; + size++; + } + + public static void Push_Front(int t) + { + front--; + if (front < 0) + { + front = 100000001 - 1; + } + + buffer[front] = t; + size++; + } + + public static void Pop_Back() + { + back--; + if (back < 0) + { + back = 100000001 - 1; + } + + size--; + } + + public static void Pop_Front() + { + front++; + if (front == 100000001) + { + front = 0; + } + + size--; + } + + public static void Clear() + { + front = 0; + back = 100000001 - 1; + size = 0; + } + + public static int Front() + { + return 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/NearSmal.cs b/CourseApp/Module4/NearSmal.cs new file mode 100644 index 0000000..1169c6a --- /dev/null +++ b/CourseApp/Module4/NearSmal.cs @@ -0,0 +1,81 @@ +using System; + +public class NearSmal +{ + public static void GetNearestSmaller() + { + var numElem = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var sVal = s.Split(' '); + var arrElem = new int[numElem]; + var ansArr = new int[numElem]; + for (int i = 0; i < numElem; i++) + { + arrElem[i] = int.Parse(sVal[i]); + } + + int ind = numElem - 1; + while (ind >= 0) + { + while (Stack.Empty() == false && arrElem[Stack.Back()] >= arrElem[ind]) + { + Stack.Pop(); + } + + if (Stack.Empty() == true) + { + ansArr[ind] = -1; + } + else + { + ansArr[ind] = Stack.Back(); + } + + Stack.Push(ind); + + ind--; + } + + for (int i = 0; i < numElem; i++) + { + Console.Write(ansArr[i] + " "); + } + } + + private class Stack + { + private static int[] buff = new int[100000001]; + private static int top = -1; + + public static void Push(int a) + { + top++; + buff[top] = a; + } + + public static void Pop() + { + top--; + } + + public static int Size() + { + return top + 1; + } + + public static bool Empty() + { + return top == -1; + } + + public static void Clear() + { + top = -1; + } + + public static int Back() + { + return buff[top]; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/Psp.cs b/CourseApp/Module4/Psp.cs new file mode 100644 index 0000000..b0ae644 --- /dev/null +++ b/CourseApp/Module4/Psp.cs @@ -0,0 +1,68 @@ +using System; + +namespace CourseApp.Module4 +{ + public class Psp + { + public static void GetPsp() + { + string parenthes = Console.ReadLine(); + int count = 0; + + for (int i = 0; i < parenthes.Length; i++) + { + if (parenthes[i] == '(') + { + Stack.Push(parenthes[i]); + } + else if (Stack.Empty() == false && parenthes[i] == ')') + { + Stack.Pop(); + } + else + { + count += 1; + } + } + + Console.WriteLine(count + Stack.Size()); + } + + private class Stack + { + private static int[] buf = new int[100000001]; + private static int tp = -1; + + public static void Push(int a) + { + tp++; + buf[tp] = a; + } + + public static void Pop() + { + tp--; + } + + public static int Size() + { + return tp + 1; + } + + public static bool Empty() + { + return tp == -1; + } + + public static void Clr() + { + tp = -1; + } + + public static int Bak() + { + return buf[tp]; + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/SORT.cs b/CourseApp/Module4/SORT.cs new file mode 100644 index 0000000..1a7d943 --- /dev/null +++ b/CourseApp/Module4/SORT.cs @@ -0,0 +1,94 @@ +using System; + +namespace CourseApp.Module4 +{ + public class SORT + { + public static void GetSorting() + { + var numbWag = int.Parse(Console.ReadLine()); + var s = Console.ReadLine(); + var sVal = s.Split(' '); + var arrWag = new int[numbWag]; + var ansArr = new int[numbWag]; + for (int i = 0; i < numbWag; i++) + { + arrWag[i] = int.Parse(sVal[i]); + } + + int megaKrut = 0; + int iamGrut = 0; + + while (megaKrut != numbWag) + { + if (Stack.Empty() == true || (iamGrut < numbWag && arrWag[iamGrut] < Stack.Back())) + { + Stack.Push(arrWag[iamGrut]); + iamGrut++; + } + else + { + ansArr[megaKrut] += Stack.Back(); + Stack.Pop(); + megaKrut++; + } + } + + bool isAnswer = true; + + for (int i = 0; i < ansArr.Length - 1; i++) + { + if (ansArr[i] > ansArr[i + 1]) + { + isAnswer = false; + } + } + + if (isAnswer == true) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + private class Stack + { + private static int[] buffer = new int[100000001]; + private static int top = -1; + + public static void Push(int a) + { + top++; + buffer[top] = a; + } + + public static void Pop() + { + top--; + } + + public static int Size() + { + return top + 1; + } + + public static bool Empty() + { + return top == -1; + } + + public static void Clear() + { + top = -1; + } + + public static int Back() + { + return buffer[top]; + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module5/Task_1/BinarTreeBranch.cs b/CourseApp/Module5/Task_1/BinarTreeBranch.cs new file mode 100644 index 0000000..94edd75 --- /dev/null +++ b/CourseApp/Module5/Task_1/BinarTreeBranch.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module5.Task_1 +{ + public class BinarTreeBranch + { + private Node root; + private List size; + + public static void BinaryTreeBranchMethod() + { + string s = Console.ReadLine(); + + string[] sValues = s.Split(' '); + + var tree = new BinarTreeBranch(); + + for (int i = 0; i < sValues.Length - 1; i++) + { + tree.Insert(int.Parse(sValues[i])); + } + + tree.FindLastElem(); + } + + public void Insert(int n) + { + root = InnerInsert(n, root); + } + + public List GetValues() + { + size = new List(); + InnerTraversal(root); + return size; + } + + private bool InnerFind(int n, Node root) + { + if (root == null) + { + return false; + } + + if (n == root.Data) + { + return true; + } + else if (n < root.Data) + { + return InnerFind(n, root.Left); + } + else + { + return InnerFind(n, root.Right); + } + } + + private bool Find(int n) + { + return InnerFind(n, root); + } + + 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 n, Node root) + { + if (root == null) + { + return new Node(n); + } + + if (root.Data > n) + { + root.Left = InnerInsert(n, root.Left); + } + else if (root.Data < n) + { + root.Right = InnerInsert(n, 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 n) + { + Data = n; + 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/Task_1/IsTreeBalanced.cs b/CourseApp/Module5/Task_1/IsTreeBalanced.cs new file mode 100644 index 0000000..2f73b30 --- /dev/null +++ b/CourseApp/Module5/Task_1/IsTreeBalanced.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; + +namespace CourseApp.Module5.Task_1 +{ + public class IsTreeBalanced + { + private Node root; + + public static void IsTreeBalancedMethod() + { + string s = Console.ReadLine(); + + string[] values = s.Split(' '); + + var tree = new IsTreeBalanced(); + + for (int i = 0; i < values.Length - 1; i++) + { + tree.Insert(int.Parse(values[i])); + } + + if (tree.IsBalanced(tree.root)) + { + Console.WriteLine("YES"); + } + else + { + Console.WriteLine("NO"); + } + } + + public void Insert(int data) + { + root = InnerInsert(data, root); + } + + private Node InnerInsert(int data, Node root) + { + if (root == null) + { + return new Node(data); + } + + if (root.Data > data) + { + root.Left = InnerInsert(data, root.Left); + } + else if (root.Data < data) + { + root.Right = InnerInsert(data, root.Right); + } + + return root; + } + + private bool IsBalanced(Node node) + { + int lh; + + int rh; + + if (node == null) + { + return true; + } + + lh = Height(node.Left); + rh = Height(node.Right); + + if (Math.Abs(lh - rh) <= 1 && IsBalanced(node.Left) + && IsBalanced(node.Right)) + { + return true; + } + + return false; + } + + private int Height(Node node) + { + if (node == null) + { + return 0; + } + + return 1 + Math.Max(Height(node.Left), Height(node.Right)); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module5/Task_1/Node.cs b/CourseApp/Module5/Task_1/Node.cs new file mode 100644 index 0000000..5017523 --- /dev/null +++ b/CourseApp/Module5/Task_1/Node.cs @@ -0,0 +1,18 @@ +namespace CourseApp.Module5.Task_1 +{ + public class Node + { + public Node(int data) + { + Data = data; + Left = null; + Right = null; + } + + public Node Left { get; set; } + + public Node Right { get; set; } + + public int Data { get; set; } + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..522646a 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(); + InversionCount.Start(); } } }