Skip to content

Commit d2c7672

Browse files
ClémentClément
authored andcommitted
Adding solution to SDictionary problem.
1 parent fc66167 commit d2c7672

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
SDictionary friends = new SDictionary(11);
8+
friends.Add("Bob", null);
9+
friends.Add("Pete", null);
10+
friends.Add("Mary", null);
11+
friends.Add("Lora", null);
12+
}
13+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
3+
public class SDictionary
4+
{
5+
private class Cell
6+
{
7+
public string Value { get; set; }
8+
public string Key { get; set; }
9+
10+
public Cell(
11+
string keyP,
12+
string valueP
13+
)
14+
{
15+
Key = keyP;
16+
Value = valueP;
17+
}
18+
19+
public override string ToString()
20+
{
21+
return Key + ":" + Value;
22+
}
23+
}
24+
25+
private Cell[] table;
26+
27+
public SDictionary(
28+
int size = 31
29+
)
30+
{
31+
table = new Cell[size];
32+
}
33+
34+
public int GetIndex(string keyP, int countP)
35+
{
36+
return ((int)(keyP[0]) + countP) % table.Length;
37+
}
38+
public void Add(string keyP, string valueP)
39+
{
40+
int count = 0;
41+
int index = GetIndex(keyP, count);
42+
while (
43+
table[index] != null
44+
)
45+
{
46+
count++;
47+
index = GetIndex(keyP, count);
48+
}
49+
table[index] = new Cell(
50+
keyP,
51+
valueP
52+
);
53+
}
54+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
SDictionary friends = new SDictionary(11);
8+
friends.Add("Bob", null);
9+
friends.Add("Pete", null);
10+
friends.Add("Mary", null);
11+
friends.Add("Lora", null);
12+
Console.WriteLine(friends);
13+
// Inserting "Lora" again.
14+
friends.Add("Lora", null);
15+
Console.WriteLine(friends);
16+
17+
Console.WriteLine("Deleted Bob:" + friends.Delete("Bob"));
18+
Console.WriteLine(friends);
19+
Console.WriteLine("Deleted Mary:" + friends.Delete("Mary"));
20+
Console.WriteLine(friends);
21+
22+
// Exhibiting Delete incorrect behavior:
23+
SDictionary demo = new SDictionary(2);
24+
string error = "Alex";
25+
demo.Add("Alice", null);
26+
demo.Add(error, null);
27+
demo.Delete("Alice");
28+
Console.WriteLine($"{error} is in dictionary: {demo.Delete(error)}.");
29+
// Done: the program will believe that "Alex" is not
30+
// in the demo array, while it is.
31+
Console.WriteLine(demo);
32+
33+
}
34+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
3+
public class SDictionary
4+
{
5+
6+
private class Cell
7+
{
8+
public string Value { get; set; }
9+
public string Key { get; set; }
10+
11+
public Cell(
12+
string keyP,
13+
string valueP
14+
)
15+
{
16+
Key = keyP;
17+
Value = valueP;
18+
}
19+
20+
public override string ToString()
21+
{
22+
return Key + ":" + Value;
23+
}
24+
}
25+
26+
private Cell[] table;
27+
28+
public SDictionary(
29+
int size = 31
30+
)
31+
{
32+
table = new Cell[size];
33+
}
34+
35+
public int GetIndex(string keyP, int countP)
36+
{
37+
return ((int)(keyP[0]) + countP) % table.Length;
38+
}
39+
public void Add(string keyP, string valueP)
40+
{
41+
int count = 0;
42+
int index = GetIndex(keyP, count);
43+
while (
44+
table[index] != null
45+
)
46+
{
47+
count++;
48+
index = GetIndex(keyP, count);
49+
}
50+
table[index] = new Cell(
51+
keyP,
52+
valueP
53+
);
54+
}
55+
56+
// Delete method:
57+
public bool Delete(string keyP)
58+
{
59+
int count = 0;
60+
int index = GetIndex(keyP, count);
61+
bool found = false;
62+
while (table[index] != null && !found)
63+
{
64+
if (table[index].Key.Equals(keyP))
65+
{
66+
found = true;
67+
table[index] = null;
68+
}
69+
count++;
70+
index = GetIndex(keyP, count);
71+
}
72+
return found;
73+
}
74+
// End of Delete method.
75+
76+
// ToString solution:
77+
public override string ToString()
78+
{
79+
string returned =
80+
"";
81+
for (int i = 0; i < table.Length; i++)
82+
{
83+
returned += String.Format("\nIndex {0, -2} |", i);
84+
if (table[i] != null)
85+
{
86+
returned += String.Format(
87+
" {0, -10}| {1, -10} |",
88+
table[i].Key,
89+
table[i].Value
90+
);
91+
}
92+
}
93+
return returned;
94+
}
95+
// End of ToString solution.
96+
}

source/order

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
./exercises/data/stacks.md
105105
./exercises/data/queues.md
106106
./exercises/data/trees.md
107+
./exercises/data/dictionary.md
107108
./exercises/control/
108109
./exercises/control/conditionals.md
109110
./exercises/control/iteratives.md

0 commit comments

Comments
 (0)