Skip to content
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
38 changes: 38 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build

on: [push]

jobs:
build-Windows:

runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- name: Build
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.x'
- name: Restore
run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {nuget restore $file.FullName}
- name: Build
run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet build $file.FullName}
- name: Test
run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName}

build-Ubuntu:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.x'
- name: Restore
run: for f in $(find . -name "*.sln"); do dotnet restore $f; done
- name: Build
run: for f in $(find . -name "*.sln"); do dotnet build $f; done
- name: Test
run: for f in $(find . -name "*.sln"); do dotnet test $f; done
31 changes: 31 additions & 0 deletions Homework2/Bor/Bor.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32210.238
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bor", "Bor\Bor.csproj", "{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BorTest", "BorTest\BorTest.csproj", "{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Release|Any CPU.Build.0 = Release|Any CPU
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EEBE94E6-045F-4E20-8C45-A352B7E2EEA7}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions Homework2/Bor/Bor/Bor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
180 changes: 180 additions & 0 deletions Homework2/Bor/Bor/Trie.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
namespace Trie;

/// <summary>
/// A class representing the Trie
/// </summary>
public class Trie
{
/// <summary>
/// A class representing the Trie node
/// </summary>
private class Node
{
/// <summary>
/// Dictionary for storing characters for each node
/// </summary>
public Dictionary<char, Node> Nodes = new();

/// <summary>
/// Node property - whether it is the end of a string
/// </summary>
public bool IsTerminal { get; set; }
}

/// <summary>
/// Bor root
/// </summary>
private readonly Node root = new();

/// <summary>
/// Bor size
/// </summary>
public int Size { get; private set; }

/// <summary>
/// Function for adding a string
/// </summary>
/// <param name="element"> Element to add </param>
/// <returns> Was there an element string before calling Add </returns>
public bool Add(string element)
{
if (Contains(element))
{
return false;
}

Node node = root;
for (int i = 0; i < element.Length; i++)
{
if (node != null && !node.Nodes.ContainsKey(element[i]))
{
node.Nodes.Add(element[i], new Node());
Size++;
}

if (node != null && node.Nodes.ContainsKey(element[i]))
{
node = node.Nodes[element[i]];
}
}

if (node != null)
{
node.IsTerminal = true;
}

return true;
}

/// <summary>
/// Is the string contained in the Trie
/// </summary>
/// <param name="element"> Element to search </param>
/// <returns> True if there is such a string. False if there is no such string </returns>
public bool Contains(string element)
{
Node? node = root;
for (int i = 0; i < element.Length; i++)
{
if (node.Nodes.TryGetValue(element[i], out node))
{
continue;
}

return false;
}

return node.IsTerminal;
}

/// <summary>
/// Function for finding the number of strings starting with a prefix
/// </summary>
/// <returns> The number of strings starting with the prefix </returns>
public int HowManyStartWithPrefix(string prefix)
{
Node? node = root;
for (int i = 0; i < prefix.Length; i++)
{
if (node != null)
{
if (node.Nodes.TryGetValue(prefix[i], out node))
{
continue;
}
}

return 0;
}

if (node == null)
{
return 0;
}

return node.IsTerminal ? 1 + node.Nodes.Count : node.Nodes.Count;
}

/// <summary>
/// Function for clearing dictionaries
/// </summary>
/// <param name="node"></param>
static private void ClearDictionaryAndNode(Node node)
{
node.Nodes.Clear();
}

/// <summary>
/// Function for deleting string from Trie
/// </summary>
/// <param name="element"> Element to delete </param>
/// <returns> as there an element string before calling Remove </returns>
public bool Remove(string element)
{
if (!Contains(element))
{
return false;
}

int index = 0;
Node node = root;
Node? parent = null;
for (int i = 0; i < element.Length; i++)
{
if (node != null)
{
node = node.Nodes[element[i]];

if (node.Nodes.Count == 0 && i == element.Length - 1)
{
if (parent != null)
{
ClearDictionaryAndNode(parent);
Size -= i - index;
return true;
}
else
{
ClearDictionaryAndNode(root);
Size = 0;
return true;
}
}

if (node.Nodes.Count != 0 && i == element.Length - 1)
{
node.IsTerminal = false;
return true;
}
}

if (node != null && node.IsTerminal)
{
index = i;
parent = node;
}
}

return true;
}
}
10 changes: 10 additions & 0 deletions Homework2/Bor/Bor/Trie.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
21 changes: 21 additions & 0 deletions Homework2/Bor/BorTest/BorTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Bor\Bor.csproj" />
</ItemGroup>

</Project>
97 changes: 97 additions & 0 deletions Homework2/Bor/BorTest/TrieTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
namespace TrieTest;

using NUnit.Framework;
using Trie;

public class TrieTest
{
private Trie trie = new();

[SetUp]
public void Setup()
{
trie = new();
}

[Test]
public void ShouldExpectedFalseWhenRemoveFromEmptyBor()
{
Assert.IsFalse(trie.Remove("hello"));
}

[Test]
public void ShouldExpectedFalseWhenAddExistingString()
{
Assert.IsTrue(trie.Add("hello"));
Assert.IsFalse(trie.Add("hello"));
}

[Test]
public void ShouldExpectedFalseWhenContainsForNonExistingString()
{
Assert.IsFalse(trie.Contains("hello"));
}

[Test]
public void ShouldExpectedTrueWhenContainsForExistingString()
{
Assert.IsTrue(trie.Add("hello"));
Assert.IsTrue(trie.Contains("hello"));
}

[Test]
public void ShouldExpectedFalseWhenRemoveForRemovedString()
{
Assert.IsTrue(trie.Add("hello"));
Assert.IsTrue(trie.Remove("hello"));
Assert.IsFalse(trie.Remove("hello"));
}

[Test]
public void ShouldExpectedFalseWhenContainsForRemovedString()
{
Assert.IsTrue(trie.Add("hello"));
Assert.IsTrue(trie.Remove("hello"));
Assert.IsFalse(trie.Contains("hello"));
}

[Test]
public void ShouldExpected5WhenSizeForBorContains5Node()
{
Assert.IsTrue(trie.Add("hello"));
Assert.AreEqual(5, trie.Size);
}

[Test]
public void ShouldBorSizeNotChangeWhenAddExistingSubstring()
{
Assert.IsTrue(trie.Add("hello"));
int size = trie.Size;
Assert.IsTrue(trie.Add("hell"));
Assert.AreEqual(size, trie.Size);
}

[Test]
public void ShouldBorSizeEqual8WhenAddStringLength3WithNonExistingFirstSymbolForBorContains5Node()
{
Assert.IsTrue(trie.Add("hello"));
Assert.IsTrue(trie.Add("bye"));
Assert.AreEqual(8, trie.Size);
}

[Test]
public void ShouldExpected2WhenHowManyStartWithPrefixForBorContains2StringWhichStartWithSamePrefix()
{
Assert.IsTrue(trie.Add("hello"));
Assert.IsTrue(trie.Add("hel"));
Assert.AreEqual(2, trie.HowManyStartWithPrefix("hel"));
}

[Test]
public void ShouldExpected0WhenHowManyStartWithPrefixForNonExistingPrefix()
{
Assert.IsTrue(trie.Add("hello"));
Assert.IsTrue(trie.Add("bye"));
Assert.AreEqual(0, trie.HowManyStartWithPrefix("leee"));
}
}