Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module 4 #48

Open
wants to merge 2 commits into
base: Evgenij_Smurov
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CourseApp.Tests/CourseApp.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<NoWarn>1573,1591,1701;1702;1705</NoWarn>
<IsPackable>false</IsPackable>
21 changes: 8 additions & 13 deletions CourseApp/CourseApp.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<NoWarn>1573,1591,1701;1702;1705;</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="all" />
</ItemGroup>

<PropertyGroup>
<CodeAnalysisRuleSet>../_stylecop/stylecop.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>../_stylecop/stylecop.ruleset</CodeAnalysisRuleSet>
<GenerateFullPaths>true</GenerateFullPaths>
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="../_stylecop/stylecop.json" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="all"/>
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="../_stylecop/stylecop.json"/>
</ItemGroup>

</Project>
</Project>
50 changes: 50 additions & 0 deletions CourseApp/Module4/CorrectBracketSequence.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
87 changes: 87 additions & 0 deletions CourseApp/Module4/Deque.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
60 changes: 60 additions & 0 deletions CourseApp/Module4/NearestMin.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
52 changes: 52 additions & 0 deletions CourseApp/Module4/SegmentionsMin.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
44 changes: 44 additions & 0 deletions CourseApp/Module4/Stack.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
54 changes: 54 additions & 0 deletions CourseApp/Module4/TrainSort.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
9 changes: 3 additions & 6 deletions CourseApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System;
using CourseApp.Module2;
using CourseApp.Module4;

namespace CourseApp
{
public class Program
{
public static void Main(string[] args)
{
BubbleSort.BubbleSortMethod();

Console.WriteLine("Hello World");
CorrectBracketSequence.CountFails();
}
}
}
}
1 change: 1 addition & 0 deletions CourseApp/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
))(((