Skip to content

Commit ed05f48

Browse files
committed
Merge remote-tracking branch 'remotes/origin/Bor' into LZW.Homework3
# Conflicts: # Homework2/Bor/Bor/Bor.cs
2 parents 3a895bb + 67f01b9 commit ed05f48

3 files changed

Lines changed: 121 additions & 132 deletions

File tree

Homework2/Bor/Bor/Bor.cs

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace BorSpace;
1+
namespace Bor;
42

53
/// <summary>
64
/// A class representing the bor data structure
@@ -13,17 +11,17 @@ public class Bor
1311
private class Node
1412
{
1513
// Dictionary for storing characters for each node
16-
public Dictionary<char, Node> dictionary = new Dictionary<char, Node>();
14+
public Dictionary<char, Node> Nodes = new();
1715

18-
// A field for storing information about whether a character is the end of a string
19-
public bool isTerminal { get; set; }
16+
// Node property - whether it is the end of a string
17+
public bool IsTerminal { get; set; }
2018
}
2119

2220
// Bor root
23-
private Node root = new();
21+
private readonly Node root = new();
2422

2523
// Bor size
26-
private int size;
24+
public int Size { get; private set; }
2725

2826
/// <summary>
2927
/// Function for adding a string
@@ -36,23 +34,27 @@ public bool Add(string element)
3634
{
3735
return false;
3836
}
39-
Node? node = root;
37+
38+
Node node = root;
4039
for (int i = 0; i < element.Length; i++)
4140
{
42-
if (node != null && !node.dictionary.ContainsKey(element[i]))
41+
if (node != null && !node.Nodes.ContainsKey(element[i]))
4342
{
44-
node.dictionary.Add(element[i], new Node());
45-
size++;
43+
node.Nodes.Add(element[i], new Node());
44+
Size++;
4645
}
47-
if (node != null && node.dictionary.ContainsKey(element[i]))
46+
47+
if (node != null && node.Nodes.ContainsKey(element[i]))
4848
{
49-
node.dictionary.TryGetValue(element[i], out node);
49+
node = node.Nodes[element[i]];
5050
}
5151
}
52+
5253
if (node != null)
5354
{
54-
node.isTerminal = true;
55+
node.IsTerminal = true;
5556
}
57+
5658
return true;
5759
}
5860

@@ -66,13 +68,14 @@ public bool Contains(string element)
6668
Node? node = root;
6769
for (int i = 0; i < element.Length; i++)
6870
{
69-
node.dictionary.TryGetValue(element[i], out node);
70-
if (node == null)
71+
if (node.Nodes.TryGetValue(element[i], out node))
7172
{
72-
return false;
73+
continue;
7374
}
75+
return false;
7476
}
75-
return node.isTerminal;
77+
78+
return node.IsTerminal;
7679
}
7780

7881
/// <summary>
@@ -86,32 +89,35 @@ public int HowManyStartWithPrefix(string prefix)
8689
{
8790
if (node != null)
8891
{
89-
node.dictionary.TryGetValue(prefix[i], out node);
90-
}
91-
else
92-
{
93-
return 0;
92+
if (node.Nodes.TryGetValue(prefix[i], out node))
93+
{
94+
continue;
95+
}
9496
}
97+
98+
return 0;
9599
}
100+
96101
if (node == null)
97102
{
98103
return 0;
99104
}
100-
return node.isTerminal ? 1 + node.dictionary.Count() : node.dictionary.Count();
105+
106+
return node.IsTerminal ? 1 + node.Nodes.Count : node.Nodes.Count;
101107
}
102108

103109
// Function for clearing dictionaries
104-
static void ClearDictionaryAndNode(string element, int index, Node? node)
110+
static private void ClearDictionaryAndNode(string element, int index, Node? node)
105111
{
106112
if (index + 1 < element.Length - 1 && node != null)
107113
{
108-
node.dictionary.TryGetValue(element[index + 1], out node);
114+
node = node.Nodes[element[index + 1]];
109115
ClearDictionaryAndNode(element, index + 1, node);
110116
}
117+
111118
if (node != null)
112119
{
113-
node.dictionary.Clear();
114-
node = null;
120+
node.Nodes.Clear();
115121
}
116122
}
117123

@@ -126,15 +132,17 @@ public bool Remove(string element)
126132
{
127133
return false;
128134
}
135+
129136
int index = 0;
130137
Node? node = root;
131138
for (int i = 0; i < element.Length; i++)
132139
{
133140
if (node != null)
134141
{
135-
node.dictionary.TryGetValue(element[i], out node);
142+
node = node.Nodes[element[i]];
136143
}
137-
string subString = element.Substring(0, i + 1);
144+
145+
string subString = element[0..(i + 1)];
138146
if (HowManyStartWithPrefix(subString) < 2)
139147
{
140148
index = i;
@@ -144,17 +152,4 @@ public bool Remove(string element)
144152
ClearDictionaryAndNode(element, index, node);
145153
return true;
146154
}
147-
148-
/// <summary>
149-
/// Function for returning the number of elements
150-
/// </summary>
151-
/// <returns> Number of elements contained in the bor </returns>
152-
public int Size()
153-
{
154-
return size;
155-
}
156-
157-
static void Main(string[] args)
158-
{
159-
}
160155
}

Homework2/Bor/Bor/Bor.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
4+
<OutputType>Library</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>

Homework2/Bor/BorTest/BorTest.cs

Lines changed: 80 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,97 @@
1-
using NUnit.Framework;
2-
3-
using BorSpace;
1+
namespace BorTest;
42

5-
using System;
3+
using NUnit.Framework;
4+
using Bor;
65

7-
namespace BorTest
6+
public class BorTest
87
{
9-
public class Tests
10-
{
11-
Bor? bor;
12-
[SetUp]
13-
public void Setup()
14-
{
15-
bor = new Bor();
16-
}
8+
Bor bor = new();
179

18-
[Test]
19-
public void DeleteLineFromEmptyBor()
20-
{
21-
Assert.IsFalse(bor?.Remove("hello"));
22-
}
10+
[SetUp]
11+
public void Setup()
12+
{
13+
bor = new();
14+
}
2315

24-
[Test]
25-
public void AddExistingString()
26-
{
27-
Assert.IsTrue(bor?.Add("hello"));
28-
Assert.IsFalse(bor?.Add("hello"));
29-
}
16+
[Test]
17+
public void ShouldExpectedFalseWhenRemoveFromEmptyBor()
18+
{
19+
Assert.IsFalse(bor.Remove("hello"));
20+
}
3021

31-
[Test]
32-
public void FindNonExistentString()
33-
{
34-
Assert.IsFalse(bor?.Contains("hello"));
35-
}
22+
[Test]
23+
public void ShouldExpectedFalseWhenAddExistingString()
24+
{
25+
Assert.IsTrue(bor.Add("hello"));
26+
Assert.IsFalse(bor.Add("hello"));
27+
}
3628

37-
[Test]
38-
public void FindStringAfterAdd()
39-
{
40-
Assert.IsTrue(bor?.Add("hello"));
41-
Assert.IsTrue(bor?.Contains("hello"));
42-
}
29+
[Test]
30+
public void ShouldExpectedFalseWhenContainsForNonExistingString()
31+
{
32+
Assert.IsFalse(bor.Contains("hello"));
33+
}
4334

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-
}
35+
[Test]
36+
public void ShouldExpectedTrueWhenContainsForExistingString()
37+
{
38+
Assert.IsTrue(bor.Add("hello"));
39+
Assert.IsTrue(bor.Contains("hello"));
40+
}
5141

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-
}
42+
[Test]
43+
public void ShouldExpectedFalseWhenRemoveForRemovedString()
44+
{
45+
Assert.IsTrue(bor.Add("hello"));
46+
Assert.IsTrue(bor.Remove("hello"));
47+
Assert.IsFalse(bor.Remove("hello"));
48+
}
5949

60-
[Test]
61-
public void FindSizeAfterAdd()
62-
{
63-
Assert.IsTrue(bor?.Add("hello"));
64-
Assert.AreEqual(bor?.Size(), 5);
65-
}
50+
[Test]
51+
public void ShouldExpectedFalseWhenContainsForRemovedString()
52+
{
53+
Assert.IsTrue(bor.Add("hello"));
54+
Assert.IsTrue(bor.Remove("hello"));
55+
Assert.IsFalse(bor.Contains("hello"));
56+
}
6657

67-
[Test]
68-
public void FindSizeAfterAddStringFromExistingSymbol()
69-
{
70-
Assert.IsTrue(bor?.Add("hello"));
71-
Assert.IsTrue(bor?.Add("hell"));
72-
Assert.AreEqual(bor?.Size(), 5);
73-
}
58+
[Test]
59+
public void ShouldExpected5WhenSizeForBorContains5Node()
60+
{
61+
Assert.IsTrue(bor.Add("hello"));
62+
Assert.AreEqual(5, bor?.Size);
63+
}
7464

75-
[Test]
76-
public void FindSizeAfterAddStringFromNonExistingSymbol()
77-
{
78-
Assert.IsTrue(bor?.Add("hello"));
79-
Assert.IsTrue(bor?.Add("bye"));
80-
Assert.AreEqual(bor?.Size(), 8);
81-
}
65+
[Test]
66+
public void ShouldBorSizeNotChangeWhenAddExistingSubstring()
67+
{
68+
Assert.IsTrue(bor.Add("hello"));
69+
int size = bor.Size;
70+
Assert.IsTrue(bor.Add("hell"));
71+
Assert.AreEqual(size, bor.Size);
72+
}
8273

83-
[Test]
84-
public void FindSizeAfterAddStringFromSomeMatchingSymbol()
85-
{
86-
Assert.IsTrue(bor?.Add("hello"));
87-
Assert.IsTrue(bor?.Add("hey"));
88-
Assert.AreEqual(bor?.Size(), 6);
89-
}
74+
[Test]
75+
public void ShouldBorSizeEqual8WhenAddStringLength3WithNonExistingFirstSymbolForBorContains5Node()
76+
{
77+
Assert.IsTrue(bor.Add("hello"));
78+
Assert.IsTrue(bor.Add("bye"));
79+
Assert.AreEqual(8, bor.Size);
80+
}
9081

91-
[Test]
92-
public void FindStringFromInvalidSymbol()
93-
{
94-
Assert.Throws<MyException>(() => bor?.Contains("ÿÿÿÿ"));
95-
}
82+
[Test]
83+
public void ShouldExpected2WhenHowManyStartWithPrefixForBorContains2StringWhichStartWithThisPrefix()
84+
{
85+
Assert.IsTrue(bor.Add("hello"));
86+
Assert.IsTrue(bor.Add("hel"));
87+
Assert.AreEqual(2, bor.HowManyStartWithPrefix("hel"));
88+
}
9689

97-
[Test]
98-
public void RemoveNonExistentString()
99-
{
100-
Assert.Throws<MyException>(() => bor?.Remove("ÿÿÿÿ"));
101-
}
90+
[Test]
91+
public void ShouldExpected0WhenHowManyStartWithPrefixForNonExistingPrefix()
92+
{
93+
Assert.IsTrue(bor.Add("hello"));
94+
Assert.IsTrue(bor.Add("bye"));
95+
Assert.AreEqual(0, bor.HowManyStartWithPrefix("leee"));
10296
}
10397
}

0 commit comments

Comments
 (0)