-
Notifications
You must be signed in to change notification settings - Fork 0
Bor #7
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
base: main
Are you sure you want to change the base?
Bor #7
Changes from 2 commits
666961b
9af2edf
480d6ba
df383e7
c159a09
b7fc6d2
fe7ace3
f294d05
2e1aa0e
2d95dc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| namespace Bor; | ||
|
|
||
| /// <summary> | ||
| /// A class representing the bor data structure | ||
| /// </summary> | ||
| public class Bor | ||
| { | ||
| /// <summary> | ||
| /// // A class representing the bor data structure | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не совсем правда, и комментарий в неправильном формате, достаточно /// |
||
| /// </summary> | ||
| private class Node | ||
| { | ||
| // Dictionary for storing characters for each node | ||
| public Dictionary<char, Node> Nodes = new(); | ||
|
|
||
| // Node property - whether it is the end of a string | ||
| public bool IsTerminal { get; set; } | ||
| } | ||
|
|
||
| // Bor root | ||
| private readonly Node root = new(); | ||
|
|
||
| // Bor size | ||
| 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 Bor | ||
| /// </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; | ||
| } | ||
|
|
||
| // Function for clearing dictionaries | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Внезапно комментарий не в стандартном формате |
||
| static private void ClearDictionaryAndNode(Node node) | ||
| { | ||
| node.Nodes.Clear(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Function for deleting string from a Bor | ||
| /// </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? anotherNode = null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я бы назвал его parent или как-то так, потому что вообще непонятна его роль сейчас |
||
| 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 (anotherNode != null) | ||
| { | ||
| ClearDictionaryAndNode(anotherNode); | ||
| 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; | ||
| anotherNode = node; | ||
| } | ||
|
|
||
| } | ||
| return true; | ||
| } | ||
| } | ||
| 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> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||
| namespace BorTest; | ||||||
|
|
||||||
| using NUnit.Framework; | ||||||
| using Bor; | ||||||
|
|
||||||
| public class BorTest | ||||||
| { | ||||||
| Bor bor = new(); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| [SetUp] | ||||||
| public void Setup() | ||||||
| { | ||||||
| bor = new(); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldExpectedFalseWhenRemoveFromEmptyBor() | ||||||
| { | ||||||
| Assert.IsFalse(bor.Remove("hello")); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldExpectedFalseWhenAddExistingString() | ||||||
| { | ||||||
| Assert.IsTrue(bor.Add("hello")); | ||||||
| Assert.IsFalse(bor.Add("hello")); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldExpectedFalseWhenContainsForNonExistingString() | ||||||
| { | ||||||
| Assert.IsFalse(bor.Contains("hello")); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldExpectedTrueWhenContainsForExistingString() | ||||||
| { | ||||||
| Assert.IsTrue(bor.Add("hello")); | ||||||
| Assert.IsTrue(bor.Contains("hello")); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldExpectedFalseWhenRemoveForRemovedString() | ||||||
| { | ||||||
| Assert.IsTrue(bor.Add("hello")); | ||||||
| Assert.IsTrue(bor.Remove("hello")); | ||||||
| Assert.IsFalse(bor.Remove("hello")); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldExpectedFalseWhenContainsForRemovedString() | ||||||
| { | ||||||
| Assert.IsTrue(bor.Add("hello")); | ||||||
| Assert.IsTrue(bor.Remove("hello")); | ||||||
| Assert.IsFalse(bor.Contains("hello")); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldExpected5WhenSizeForBorContains5Node() | ||||||
| { | ||||||
| Assert.IsTrue(bor.Add("hello")); | ||||||
| Assert.AreEqual(5, bor.Size); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldBorSizeNotChangeWhenAddExistingSubstring() | ||||||
| { | ||||||
| Assert.IsTrue(bor.Add("hello")); | ||||||
| int size = bor.Size; | ||||||
| Assert.IsTrue(bor.Add("hell")); | ||||||
| Assert.AreEqual(size, bor.Size); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldBorSizeEqual8WhenAddStringLength3WithNonExistingFirstSymbolForBorContains5Node() | ||||||
| { | ||||||
| Assert.IsTrue(bor.Add("hello")); | ||||||
| Assert.IsTrue(bor.Add("bye")); | ||||||
| Assert.AreEqual(8, bor.Size); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldExpected2WhenHowManyStartWithPrefixForBorContains2StringWhichStartWithSamePrefix() | ||||||
| { | ||||||
| Assert.IsTrue(bor.Add("hello")); | ||||||
| Assert.IsTrue(bor.Add("hel")); | ||||||
| Assert.AreEqual(2, bor.HowManyStartWithPrefix("hel")); | ||||||
| } | ||||||
|
|
||||||
| [Test] | ||||||
| public void ShouldExpected0WhenHowManyStartWithPrefixForNonExistingPrefix() | ||||||
| { | ||||||
| Assert.IsTrue(bor.Add("hello")); | ||||||
| Assert.IsTrue(bor.Add("bye")); | ||||||
| Assert.AreEqual(0, bor.HowManyStartWithPrefix("leee")); | ||||||
| } | ||||||
| } | ||||||
| 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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Он не Bor, он Trie