Skip to content

Commit 666961b

Browse files
committed
writing basic functions and tests
1 parent 6122f55 commit 666961b

5 files changed

Lines changed: 301 additions & 0 deletions

File tree

Homework2/Bor/Bor.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32210.238
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bor", "Bor\Bor.csproj", "{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BorTest", "BorTest\BorTest.csproj", "{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{A5948E4C-54A6-49B2-AD93-A2C768CF3B7B}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{82DAD3CF-7041-4300-B987-4CF0FD1C3E52}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {EEBE94E6-045F-4E20-8C45-A352B7E2EEA7}
30+
EndGlobalSection
31+
EndGlobal

Homework2/Bor/Bor/Bor.cs

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
using System;
2+
3+
namespace String;
4+
5+
/// <summary>
6+
/// A class representing the bor data structure
7+
/// The bor can store Latin alphabet characters, numbers
8+
/// </summary>
9+
public class Bor
10+
{
11+
/// <summary>
12+
/// // A class representing the bor data structure
13+
/// </summary>
14+
private class Node
15+
{
16+
/// <summary>
17+
/// Array of vertices to move from one vertex to another
18+
/// The bor can store Latin alphabet characters, numbers
19+
/// </summary>
20+
public Node?[] next = new Node['z'];
21+
22+
// A field for storing information about whether a character is the end of a string
23+
public bool isTerminal { get; set; }
24+
25+
// A field for storing information about the number of strings containing a certain prefix
26+
public int numberOfLinesContainingThePrefix { get; set; }
27+
}
28+
29+
private Node root = new Node();
30+
31+
// Bor size
32+
private int size;
33+
34+
/// <summary>
35+
/// Function for adding a string
36+
/// </summary>
37+
/// <param name="element"> Element to add </param>
38+
/// <returns> Was there an element string before calling Add </returns>
39+
public bool Add(string element)
40+
{
41+
Node? node = root;
42+
int counter = 0;
43+
for(int i = 0; i < element.Length; i++)
44+
{
45+
if(element[i] > 'z')
46+
{
47+
throw new Exception("The bor can store Latin alphabet characters, numbers"); ;
48+
}
49+
if (node != null && node.next[element[i]] == null)
50+
{
51+
node.next[element[i]] = new Node();
52+
size++;
53+
}
54+
else
55+
{
56+
counter++;
57+
}
58+
if (node != null)
59+
{
60+
node = node.next[element[i]];
61+
if(node != null)
62+
{
63+
node.numberOfLinesContainingThePrefix++;
64+
}
65+
}
66+
}
67+
if (counter == element.Length && node != null && node.isTerminal)
68+
{
69+
Node? newNode = root;
70+
for (int i = 0; i < element.Length; i++)
71+
{
72+
newNode = newNode?.next[element[i]];
73+
if(newNode != null)
74+
{
75+
newNode.numberOfLinesContainingThePrefix--;
76+
}
77+
}
78+
return false;
79+
}
80+
if (node != null)
81+
{
82+
node.isTerminal = true;
83+
}
84+
return true;
85+
}
86+
87+
/// <summary>
88+
/// Is the string contained in the Bor
89+
/// </summary>
90+
/// <param name="element"> Element to search </param>
91+
/// <returns> True if there is such a string. False if there is no such string </returns>
92+
public bool Contains(string element)
93+
{
94+
Node? node = root;
95+
for (int i = 0; i < element.Length; i++)
96+
{
97+
node = node.next[element[i]];
98+
if (node == null)
99+
{
100+
return false;
101+
}
102+
}
103+
return node.isTerminal;
104+
}
105+
106+
/// <summary>
107+
/// Function for finding the number of strings starting with a prefix
108+
/// </summary>Количество строк содеражащих префикс
109+
/// <returns> The number of strings starting with the prefix </returns>
110+
public int HowManyStartWithPrefix(string prefix)
111+
{
112+
Node? node = root;
113+
for (int i = 0; i < prefix.Length;i++)
114+
{
115+
if (node?.next[prefix[i]] != null)
116+
{
117+
node = node.next[prefix[i]];
118+
}
119+
else
120+
{
121+
return 0;
122+
}
123+
}
124+
return node == null ? 0 : node.numberOfLinesContainingThePrefix;
125+
}
126+
127+
/// <summary>
128+
/// Function for deleting string from a Bor
129+
/// </summary>
130+
/// <param name="element"> Element to delete </param>
131+
/// <returns> as there an element string before calling Remove </returns>
132+
public bool Remove(string element)
133+
{
134+
if(!this.Contains(element))
135+
{
136+
return false;
137+
}
138+
Node? node = root;
139+
Node? nodeWithMaxPrefix = root;
140+
for (int i = 0; i < element.Length; i++)
141+
{
142+
node = node?.next[element[i]];
143+
if(node != null)
144+
{
145+
node.numberOfLinesContainingThePrefix--;
146+
}
147+
if (i == element.Length - 1)
148+
{
149+
nodeWithMaxPrefix.isTerminal = false;
150+
}
151+
}
152+
node = root;
153+
for (int i = 0; i < element.Length; i++)
154+
{
155+
nodeWithMaxPrefix = node;
156+
node = node?.next[element[i]];
157+
if (nodeWithMaxPrefix?.next[element[i]]?.numberOfLinesContainingThePrefix == 0)
158+
{
159+
nodeWithMaxPrefix.next[element[i]] = null;
160+
}
161+
162+
}
163+
return true;
164+
}
165+
166+
/// <summary>
167+
/// Function for returning the number of elements
168+
/// </summary>
169+
/// <returns> Number of elements contained in the bor </returns>
170+
public int Size()
171+
{
172+
return this.size;
173+
}
174+
175+
static void Main(string[] args)
176+
{
177+
return;
178+
}
179+
}

Homework2/Bor/Bor/Bor.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

Homework2/Bor/BorTest/BorTest.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using NUnit.Framework;
2+
3+
using String;
4+
5+
using System;
6+
7+
namespace BorTest
8+
{
9+
public class Tests
10+
{
11+
Bor? bor;
12+
[SetUp]
13+
public void Setup()
14+
{
15+
bor = new Bor();
16+
}
17+
18+
[Test]
19+
public void DeleteLineFromEmptyBor()
20+
{
21+
Assert.IsFalse(bor?.Remove("hello"));
22+
}
23+
24+
[Test]
25+
public void AddExistingString()
26+
{
27+
Assert.IsTrue(bor?.Add("hello"));
28+
Assert.IsFalse(bor?.Add("hello"));
29+
}
30+
31+
[Test]
32+
public void FindNonExistentString()
33+
{
34+
Assert.IsFalse(bor?.Contains("hello"));
35+
}
36+
37+
[Test]
38+
public void FindStringAfterAdd()
39+
{
40+
Assert.IsTrue(bor?.Add("hello"));
41+
Assert.IsTrue(bor?.Contains("hello"));
42+
}
43+
44+
[Test]
45+
public void RemoveStringAfterRemove()
46+
{
47+
Assert.IsTrue(bor?.Add("hello"));
48+
Assert.IsTrue(bor?.Remove("hello"));
49+
Assert.IsFalse(bor?.Remove("hello"));
50+
}
51+
52+
[Test]
53+
public void FindStringAfterRemove()
54+
{
55+
Assert.IsTrue(bor?.Add("hello"));
56+
Assert.IsTrue(bor?.Remove("hello"));
57+
Assert.IsFalse(bor?.Contains("hello"));
58+
}
59+
}
60+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12+
<PackageReference Include="NUnit" Version="3.13.2" />
13+
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
14+
<PackageReference Include="coverlet.collector" Version="3.1.0" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\Bor\Bor.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)