From c3d2963d74a983af8e7984030f847e8ab1be1fda Mon Sep 17 00:00:00 2001 From: EwvwGeN Date: Wed, 13 Apr 2022 11:32:58 +0300 Subject: [PATCH 1/2] edit settings of .net --- CourseApp.Tests/CourseApp.Tests.csproj | 2 +- CourseApp/CourseApp.csproj | 21 ++++++++------------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index ae46394..79653f6 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + netcoreapp3.1 True 1573,1591,1701;1702;1705 false diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index 9551450..3634a9e 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -1,23 +1,18 @@ - - + Exe - netcoreapp2.1 + netcoreapp3.1 True 1573,1591,1701;1702;1705; - - - - - - ../_stylecop/stylecop.ruleset + ../_stylecop/stylecop.ruleset true - - + + + + - - + \ No newline at end of file From fa4bff41f4c6a4fbf35e9ba58c92aed3b1698acc Mon Sep 17 00:00:00 2001 From: EwvwGeN Date: Wed, 13 Apr 2022 11:52:57 +0300 Subject: [PATCH 2/2] completed module 4 --- CourseApp/Module4/CorrectBracketSequence.cs | 50 ++++++++++++ CourseApp/Module4/Deque.cs | 87 +++++++++++++++++++++ CourseApp/Module4/NearestMin.cs | 60 ++++++++++++++ CourseApp/Module4/SegmentionsMin.cs | 52 ++++++++++++ CourseApp/Module4/Stack.cs | 44 +++++++++++ CourseApp/Module4/TrainSort.cs | 54 +++++++++++++ CourseApp/Program.cs | 9 +-- CourseApp/input.txt | 1 + 8 files changed, 351 insertions(+), 6 deletions(-) create mode 100644 CourseApp/Module4/CorrectBracketSequence.cs create mode 100644 CourseApp/Module4/Deque.cs create mode 100644 CourseApp/Module4/NearestMin.cs create mode 100644 CourseApp/Module4/SegmentionsMin.cs create mode 100644 CourseApp/Module4/Stack.cs create mode 100644 CourseApp/Module4/TrainSort.cs create mode 100644 CourseApp/input.txt diff --git a/CourseApp/Module4/CorrectBracketSequence.cs b/CourseApp/Module4/CorrectBracketSequence.cs new file mode 100644 index 0000000..7648f56 --- /dev/null +++ b/CourseApp/Module4/CorrectBracketSequence.cs @@ -0,0 +1,50 @@ +using System; +using System.IO; + +namespace CourseApp.Module4 +{ + public class CorrectBracketSequence + { + public static void CountFails() + { + StreamReader reader = new StreamReader("input.txt"); + string data = reader.ReadLine(); + reader.Close(); + + Stack bracket = new Stack(data.Length); + int fails = 0; + foreach (char item in data) + { + switch (item) + { + case '(': + { + bracket.Push(Convert.ToInt32('(')); + } + + break; + case ')': + { + if (!bracket.Empty()) + { + bracket.Back(); + bracket.Pop(); + } + else + { + fails++; + } + } + + break; + default: + break; + } + } + + StreamWriter output = new StreamWriter("output.txt"); + output.WriteLine(bracket.Size() + fails); + output.Close(); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/Deque.cs b/CourseApp/Module4/Deque.cs new file mode 100644 index 0000000..c0cbc8d --- /dev/null +++ b/CourseApp/Module4/Deque.cs @@ -0,0 +1,87 @@ +namespace CourseApp.Module4 +{ + public class Deque + { + private int[] buffer; + private int head; + private int tail; + private int size = 0; + + public Deque (int in_size) + { + buffer = new int[in_size]; + head = in_size / 2; + tail = head - 1; + } + + public void PushFront(int input) + { + size++; + head--; + if (head == -1) + { + head = buffer.Length - 1; + } + + buffer[head] = input; + } + + public void PushBack(int input) + { + size++; + tail++; + if (tail == buffer.Length) + { + tail = 0; + } + + buffer[tail] = input; + } + + public void PopFront() + { + size--; + head++; + if (head == buffer.Length) + { + head = 0; + } + } + + public void PopBack() + { + size--; + tail--; + if (tail == -1) + { + tail = buffer.Length - 1; + } + } + + public int Front() + { + return buffer[head]; + } + + public int Back() + { + return buffer[tail]; + } + + public bool Empty() + { + return size == 0; + } + + public int Size() + { + return size; + } + + public void Clear() + { + head = buffer.Length / 2; + tail = head - 1; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/NearestMin.cs b/CourseApp/Module4/NearestMin.cs new file mode 100644 index 0000000..6a5c6ea --- /dev/null +++ b/CourseApp/Module4/NearestMin.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using System.Linq; + +namespace CourseApp.Module4 +{ + public class NearestMin + { + public static void FindNearMin() + { + StreamReader reader = new StreamReader("input.txt"); + int size = int.Parse(reader.ReadLine()); + int[] numbers = reader.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + reader.Close(); + + int[] res = new int[size]; + Stack n_mins = new Stack(size); + n_mins.Push(numbers[size - 1]); + res[size - 1] = -1; + Stack del = new Stack(size); + del.Push(0); + for (int i = size - 2; i >= 0; i--) + { + int temp = 0; + int buf1 = numbers[i]; + if (buf1 <= n_mins.Back()) + { + while ((!n_mins.Empty()) ? buf1 <= n_mins.Back() : false) + { + temp += del.Back() + 1; + del.Pop(); + n_mins.Pop(); + } + + if (n_mins.Empty()) + { + res[i] = -1; + del.Push(0); + } + else + { + res[i] = i + temp + 1; + } + } + else + { + res[i] = i + 1; + } + + n_mins.Push(buf1); + del.Push(temp); + } + + n_mins.Clear(); + StreamWriter output = new StreamWriter("output.txt"); + output.WriteLine(string.Join(" ", res)); + output.Close(); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/SegmentionsMin.cs b/CourseApp/Module4/SegmentionsMin.cs new file mode 100644 index 0000000..12322c7 --- /dev/null +++ b/CourseApp/Module4/SegmentionsMin.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; +using System.Linq; + +namespace CourseApp.Module4 +{ + public class SegmentionsMin + { + public static void FindWinMin() + { + StreamReader reader = new StreamReader("input.txt"); + int[] sizes = reader.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + int n_size = sizes[0]; + int k_size = sizes[1]; + int[] numbers = reader.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + reader.Close(); + Deque mins = new Deque(n_size); + for (int i = 0; i < k_size; i++) + { + int current = numbers[i]; + while (!mins.Empty() && mins.Back() > current) + { + mins.PopBack(); + } + + mins.PushBack(current); + } + + StreamWriter output = new StreamWriter("output.txt"); + output.WriteLine(mins.Front()); + for (int i = k_size; i < n_size; i++) + { + int current = numbers[i]; + int remove = numbers[i - k_size]; + while (!mins.Empty() && mins.Back() > current) + { + mins.PopBack(); + } + + mins.PushBack(current); + if (!mins.Empty() && mins.Front() == remove) + { + mins.PopFront(); + } + + output.WriteLine(mins.Front()); + } + + output.Close(); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/Stack.cs b/CourseApp/Module4/Stack.cs new file mode 100644 index 0000000..09f25bc --- /dev/null +++ b/CourseApp/Module4/Stack.cs @@ -0,0 +1,44 @@ +namespace CourseApp.Module4 +{ + public class Stack + { + private int[] buffer; + private int top = -1; + + public Stack (int size) + { + buffer = new int[size]; + } + + public void Push(int input) + { + top++; + buffer[top] = input; + } + + public void Pop() + { + top--; + } + + public int Back() + { + return buffer[top]; + } + + public bool Empty() + { + return top == -1; + } + + public int Size() + { + return top + 1; + } + + public void Clear() + { + top = -1; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module4/TrainSort.cs b/CourseApp/Module4/TrainSort.cs new file mode 100644 index 0000000..6b62c5e --- /dev/null +++ b/CourseApp/Module4/TrainSort.cs @@ -0,0 +1,54 @@ +using System; +using System.IO; +using System.Linq; + +namespace CourseApp.Module4 +{ + public class TrainSort + { + public static void Sort() + { + StreamReader reader = new StreamReader("input.txt"); + int size = int.Parse(reader.ReadLine()); + int[] train = reader.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + reader.Close(); + + int[] sorted_train = new int[size]; + int buffer = 0; + int search = 1; + Stack sort_vein = new Stack(size); + for (int i = 0; i < size; i++) + { + sort_vein.Push(train[i]); + if (train[i] == search) + { + sorted_train[buffer] = sort_vein.Back(); + sort_vein.Pop(); + buffer++; + search++; + while (sort_vein.Empty() ? false : sort_vein.Back() == search) + { + sorted_train[buffer] = sort_vein.Back(); + sort_vein.Pop(); + buffer++; + search++; + } + } + } + + bool trigger = true; + for (int i = 0; i < size - 1; i++) + { + if (sorted_train[i] > sorted_train[i + 1]) + { + trigger = false; + break; + } + } + + StreamWriter output = new StreamWriter("output.txt"); + output.WriteLine(trigger ? "YES" : "NO"); + output.Close(); + } + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..59afc0a 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,5 +1,4 @@ -using System; -using CourseApp.Module2; +using CourseApp.Module4; namespace CourseApp { @@ -7,9 +6,7 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - - Console.WriteLine("Hello World"); + CorrectBracketSequence.CountFails(); } } -} +} \ No newline at end of file diff --git a/CourseApp/input.txt b/CourseApp/input.txt new file mode 100644 index 0000000..65cee87 --- /dev/null +++ b/CourseApp/input.txt @@ -0,0 +1 @@ +))((( \ No newline at end of file