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 3 #47

Open
wants to merge 2 commits into
base: Evgenij_Smurov
Choose a base branch
from
Open
Show file tree
Hide file tree
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>
Expand Down
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>
74 changes: 74 additions & 0 deletions CourseApp/Module3/CyclicShift.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CourseApp.Module3
{
public class CyclicShift
{
public static void FindStringEntry()
{
StreamReader reader = new StreamReader("input.txt");
string data = reader.ReadLine();
string key = reader.ReadLine();
reader.Close();

long simp_numb = 9999993;
int alphabet = 26;
int count = RabinKarpAlgorythm(data, key, alphabet, simp_numb);
StreamWriter output = new StreamWriter("output.txt");
output.WriteLine(count);
output.Close();
}

public static int RabinKarpAlgorythm(string text, string key, int alphabet, long simp_numb)
{
int count = 0;
long basis = 1;
int size = text.Length;
text = text + text;
for (int i = 0; i < size - 1; i++)
{
basis = (basis * alphabet) % simp_numb;
}

long hash_key = 0;
long hash_text = 0;
for (int i = size - 1; i >= 0; i--)
{
hash_key = ((hash_key * alphabet) + key[i]) % simp_numb;
hash_text = ((hash_text * alphabet) + text[i]) % simp_numb;
}

for (int i = size; i >= 0; i--)
{
bool trigger = true;
if (hash_key == hash_text)
{
for (int j = 0; j < size; j++)
{
if (key[j] != text[i + j])
{
trigger = false;
}
}

if (trigger)
{
return count;
}
}

count++;
if (i != 0)
{
hash_text = ((alphabet * (hash_text - (text[i + size - 1] * basis))) + text[i - 1]) % simp_numb;
hash_text = (hash_text + simp_numb) % simp_numb;
}
}

return -1;
}
}
}
45 changes: 45 additions & 0 deletions CourseApp/Module3/CyclicSubString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CourseApp.Module3
{
public class CyclicSubString
{
public static void FindStringEntry()
{
StreamReader reader = new StreamReader("input.txt");
string data = reader.ReadLine();
reader.Close();
StreamWriter output = new StreamWriter("output.txt");
output.WriteLine(data.Length - Prefix(data)[data.Length - 1]);
output.Close();
}

public static int[] Prefix(string input)
{
int[] result = new int[input.Length];
result[0] = 0;
for (int i = 0; i < input.Length - 1; i++)
{
int buffer = result[i];
while (buffer > 0 && input[i + 1] != input[buffer])
{
buffer = result[buffer - 1];
}

if (input[i + 1] == input[buffer])
{
result[i + 1] = buffer + 1;
}
else
{
result[i + 1] = 0;
}
}

return result;
}
}
}
73 changes: 73 additions & 0 deletions CourseApp/Module3/RabinKarp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CourseApp.Module3
{
public class RabinKarp
{
public static void FindStringEntry()
{
StreamReader reader = new StreamReader("input.txt");
string data = reader.ReadLine();
string pattern = reader.ReadLine();
reader.Close();

int simp_numb = 117;
int alphabet = 26;
List<int> index = new List<int>();
RabinKarpAlgorythm(index, data, pattern, alphabet, simp_numb);
StreamWriter output = new StreamWriter("output.txt");
output.WriteLine(string.Join(" ", index));
output.Close();
}

public static void RabinKarpAlgorythm(List<int> index, string text, string pattern, int alphabet, int simp_numb)
{
int basis = 1;
int txt_size = text.Length;
int pat_size = pattern.Length;
for (int i = 0; i < pat_size - 1; i++)
{
basis = (basis * alphabet) % simp_numb;
}

int hash_pat = 0;
int hash_text = 0;
for (int i = 0; i < pat_size; i++)
{
hash_pat = ((hash_pat * alphabet) + pattern[i]) % simp_numb;
hash_text = ((hash_text * alphabet) + text[i]) % simp_numb;
}

bool trigger = true;
for (int i = 0; i <= txt_size - pat_size; i++)
{
trigger = true;
if (hash_pat == hash_text)
{
for (int j = 0; j < pat_size; j++)
{
if (pattern[j] != text[i + j])
{
trigger = false;
break;
}
}

if (trigger)
{
index.Add(i);
}
}

if (i < txt_size - pat_size)
{
hash_text = ((alphabet * (hash_text - (text[i] * basis))) + text[i + pat_size]) % simp_numb;
hash_text = (hash_text + simp_numb) % simp_numb;
}
}
}
}
}
73 changes: 73 additions & 0 deletions CourseApp/Module3/StringPeriod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.IO;

namespace CourseApp.Module3
{
public class StringPeriod
{
public static void FindStringEntry()
{
StreamReader reader = new StreamReader("input.txt");
string data = reader.ReadLine();
reader.Close();

long simp_numb = 9999993;
int alphabet = 26;
int count = GetCountOfRepeating(data, alphabet, simp_numb);
StreamWriter output = new StreamWriter("output.txt");
output.WriteLine(count);
output.Close();
}

public static int GetCountOfRepeating(string text, int alphabet, long simp_numb)
{
int size = text.Length;
int count = 1;

for (int i = 2; i <= (size / 2) + 1; i++)
{
if (size % i != 0)
{
continue;
}

count = CheckEqual(i, size, count, text, alphabet, simp_numb);
}

count = CheckEqual(size, size, count, text, alphabet, simp_numb);

return count;
}

public static int CheckEqual(int i, int size, int count, string text, int alphabet, long simp_numb)
{
long[] buffer = new long[i];
for (int j = 0; j < buffer.Length; j++)
{
long hash = 0;
for (int k = (size / i) * j; k < ((size / i) * j) + (size / i); k++)
{
hash = ((hash * alphabet) + text[k]) % simp_numb;
}

buffer[j] = hash;
if (j > 0)
{
if (buffer[j] != buffer[j - 1])
{
break;
}
}
}

if (size > 1)
{
if ((buffer[buffer.Length - 1] != 0) && (buffer[buffer.Length - 1] == buffer[buffer.Length - 2]))
{
count = i;
}
}

return count;
}
}
}
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.Module3;

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

Console.WriteLine("Hello World");
RabinKarp.FindStringEntry();
}
}
}
}
2 changes: 2 additions & 0 deletions CourseApp/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ababbababa
aba
5 changes: 5 additions & 0 deletions CourseApp/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1
2
2
3
1