Skip to content

Commit f22c278

Browse files
author
Clément
committed
Adding solution to last project.
1 parent 9a8ef6c commit f22c278

5 files changed

Lines changed: 365 additions & 6 deletions

File tree

source/code/projects/CList/CList/CList.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ public int CountSuccessive(T dataP)
236236
return mCount;
237237
}
238238

239-
// Method to remove at a particular index
240-
// Very similar to RemoveI, simply
241-
// implemented with a different philosophy.
242-
public void RemoveAt(int index)
239+
// Method to remove at a particular index
240+
// Very similar to RemoveI, simply
241+
// implemented with a different philosophy.
242+
public void RemoveAt(int index)
243243
{
244244
if (index >= 0 && index < Size)
245245
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.IO;
3+
4+
class Program
5+
{
6+
static void Main()
7+
{
8+
/*
9+
* We first create three
10+
* string variables where
11+
* our three demo files
12+
* will be created.
13+
*/
14+
string directoryPath = AppDomain
15+
.CurrentDomain
16+
.BaseDirectory;
17+
string ex0 = Path.Combine(directoryPath, "ex0.txt");
18+
string ex1 = Path.Combine(directoryPath, "ex1.txt");
19+
string ex2 = Path.Combine(directoryPath, "ex2.txt");
20+
// We now call the "Init" method to create those files.
21+
TextFileHelper.Init(ex0, ex1, ex2);
22+
23+
/*
24+
* The following is for demo purpose.
25+
*/
26+
Console.WriteLine("*****\n* Testing Display\n*****");
27+
TextFileHelper.Display(ex0);
28+
Console.WriteLine(
29+
"*****\n* Testing DisplayNoComment\n*****"
30+
);
31+
TextFileHelper.DisplayNoComment(ex0);
32+
Console.WriteLine("*****\n* Testing Reverse\n*****");
33+
string ex3 = Path.Combine(directoryPath, "ex3.txt");
34+
TextFileHelper.Reverse(ex0, ex3);
35+
TextFileHelper.Display(ex3);
36+
Console.WriteLine("*****\n* Testing Concat\n*****");
37+
string ex4 = Path.Combine(directoryPath, "ex4.txt");
38+
TextFileHelper.Concat(ex0, ex1, ex4);
39+
TextFileHelper.Display(ex4);
40+
41+
Console.WriteLine("*****\n* Testing Balanced\n*****");
42+
bool wb0 = TextFileHelper.Balanced(ex0);
43+
bool wb1 = TextFileHelper.Balanced(ex1);
44+
bool wb2 = TextFileHelper.Balanced(ex2);
45+
bool wb3 = TextFileHelper.Balanced(ex3);
46+
47+
Console.WriteLine("ex0 is balanced: " + wb0);
48+
Console.WriteLine("ex1 is balanced: " + wb1);
49+
Console.WriteLine("ex2 is balanced: " + wb2);
50+
Console.WriteLine("ex3 is balanced: " + wb3);
51+
}
52+
}
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
static class TextFileHelper
6+
{
7+
// This method creates three files at the paths
8+
// ex0P, ex1P and ex2P
9+
// that will serve for our tests.
10+
public static void Init(
11+
string ex0P,
12+
string ex1P,
13+
string ex2P,
14+
bool over = false
15+
)
16+
{
17+
if (
18+
!over
19+
&& (
20+
File.Exists(ex0P)
21+
|| File.Exists(ex1P)
22+
|| File.Exists(ex0P)
23+
)
24+
)
25+
{
26+
Console.WriteLine(
27+
"One of the path already contains a file."
28+
+ "\nCall the method with an additional parameter"
29+
+ "\nset to \"true\" to override the existing file."
30+
);
31+
}
32+
else
33+
{
34+
try
35+
{
36+
StreamWriter ex0 = new StreamWriter(ex0P);
37+
ex0.Write(
38+
"This is the first demo file\n# This line starts with an \"#\").\n(An isolated parenthesis"
39+
);
40+
ex0.Close();
41+
}
42+
catch
43+
{
44+
Console.WriteLine(
45+
"There was an error creating ex0"
46+
);
47+
}
48+
try
49+
{
50+
StreamWriter ex1 = new StreamWriter(ex1P);
51+
ex1.Write(
52+
"(This is the second demo file"
53+
+ "\n)There are parenthesis in it, but no pound (\"#\") character on the first line."
54+
);
55+
ex1.Close();
56+
}
57+
catch
58+
{
59+
Console.WriteLine(
60+
"There was an error creating ex1"
61+
);
62+
}
63+
try
64+
{
65+
StreamWriter ex2 = new StreamWriter(ex2P);
66+
ex2.Write(
67+
"# Just a comment )"
68+
+ "\nand unbalanced parentheses ("
69+
);
70+
ex2.Close();
71+
}
72+
catch
73+
{
74+
Console.WriteLine(
75+
"There was an error creating ex2"
76+
);
77+
}
78+
}
79+
}
80+
81+
public static void Display(string ex0P)
82+
{
83+
if (!File.Exists(ex0P))
84+
{
85+
Console.WriteLine(
86+
"Nothing to display: the file does not exists."
87+
);
88+
}
89+
else
90+
{
91+
try
92+
{
93+
StreamReader sw = new StreamReader(ex0P);
94+
string cLine = sw.ReadLine();
95+
while (cLine != null)
96+
{
97+
Console.WriteLine(cLine);
98+
cLine = sw.ReadLine();
99+
}
100+
}
101+
catch
102+
{
103+
Console.WriteLine(
104+
"An error was thrown from Display method."
105+
);
106+
}
107+
}
108+
}
109+
110+
public static void DisplayNoComment(string ex0P)
111+
{
112+
if (!File.Exists(ex0P))
113+
{
114+
Console.WriteLine(
115+
"Nothing to display: the file does not exists."
116+
);
117+
}
118+
else
119+
{
120+
try
121+
{
122+
StreamReader sw = new StreamReader(ex0P);
123+
string cLine = sw.ReadLine();
124+
while (cLine != null)
125+
{
126+
if (!cLine.StartsWith("#"))
127+
{
128+
Console.WriteLine(cLine);
129+
}
130+
cLine = sw.ReadLine();
131+
}
132+
}
133+
catch
134+
{
135+
Console.WriteLine(
136+
"An error was thrown from DisplayNoComment method."
137+
);
138+
}
139+
}
140+
}
141+
142+
public static void Reverse(
143+
string ex0P,
144+
string ex1P,
145+
bool over = false
146+
)
147+
{
148+
if (!File.Exists(ex0P))
149+
{
150+
Console.WriteLine(
151+
"There is a problem: the source file does not exist."
152+
);
153+
}
154+
else if (File.Exists(ex1P) && !over)
155+
{
156+
Console.WriteLine(
157+
"There is a problem: the target file already exists."
158+
+ "\nCall the method with an additional parameter"
159+
+ "\nset to \"true\" to override the existing file."
160+
);
161+
}
162+
else
163+
{
164+
List<string> contentex0P = new List<string>();
165+
try
166+
{
167+
StreamReader sw0 = new StreamReader(ex0P);
168+
string cLine = sw0.ReadLine();
169+
while (cLine != null)
170+
{
171+
contentex0P.Add(cLine);
172+
cLine = sw0.ReadLine();
173+
}
174+
sw0.Close();
175+
176+
StreamWriter sr1 = new StreamWriter(ex1P);
177+
for (int i = contentex0P.Count - 1; i >= 0; i--)
178+
{
179+
sr1.WriteLine(contentex0P[i]);
180+
}
181+
sr1.Close();
182+
}
183+
catch
184+
{
185+
Console.WriteLine(
186+
"An error was thrown from the Reverse method."
187+
);
188+
}
189+
}
190+
}
191+
192+
public static void Concat(
193+
string ex0P,
194+
string ex1P,
195+
string ex2P,
196+
bool over = false
197+
)
198+
{
199+
if (!File.Exists(ex0P) || !File.Exists(ex1P))
200+
{
201+
Console.WriteLine(
202+
"There is a problem: one of the source file does not exist."
203+
);
204+
}
205+
else if (File.Exists(ex2P) && !over)
206+
{
207+
Console.WriteLine(
208+
"There is a problem: the target file already exists."
209+
+ "\nCall the method with an additional parameter"
210+
+ "\nset to \"true\" to override the existing file."
211+
);
212+
}
213+
else
214+
{
215+
List<string> contentex0P = new List<string>();
216+
try
217+
{
218+
StreamReader sw0 = new StreamReader(ex0P);
219+
string cLine = sw0.ReadLine();
220+
while (cLine != null)
221+
{
222+
contentex0P.Add(cLine);
223+
cLine = sw0.ReadLine();
224+
}
225+
sw0.Close();
226+
227+
StreamReader sw1 = new StreamReader(ex1P);
228+
cLine = sw1.ReadLine();
229+
while (cLine != null)
230+
{
231+
contentex0P.Add(cLine);
232+
cLine = sw1.ReadLine();
233+
}
234+
sw1.Close();
235+
236+
StreamWriter sr1 = new StreamWriter(ex2P);
237+
for (int i = 0; i < contentex0P.Count; i++)
238+
{
239+
sr1.WriteLine(contentex0P[i]);
240+
}
241+
sr1.Close();
242+
}
243+
catch
244+
{
245+
Console.WriteLine(
246+
"An error was thrown from the Concat method."
247+
);
248+
}
249+
}
250+
}
251+
252+
public static bool Balanced(string ex0P)
253+
{
254+
int count = -1;
255+
if (!File.Exists(ex0P))
256+
{
257+
Console.WriteLine(
258+
"There is a problem: the source file does not exist."
259+
);
260+
}
261+
else
262+
{
263+
try
264+
{
265+
StreamReader sw0 = new StreamReader(ex0P);
266+
count = 0;
267+
char[] cLineChar;
268+
string cLine = sw0.ReadLine();
269+
while (cLine != null)
270+
{
271+
cLineChar = cLine.ToCharArray();
272+
for (int i = 0; i < cLineChar.Length; i++)
273+
{
274+
if (cLineChar[i] == '(')
275+
{
276+
count++;
277+
}
278+
if (cLineChar[i] == ')')
279+
{
280+
if (count <= 0)
281+
{
282+
return false;
283+
}
284+
else
285+
{
286+
count--;
287+
}
288+
}
289+
}
290+
cLine = sw0.ReadLine();
291+
}
292+
sw0.Close();
293+
}
294+
catch
295+
{
296+
Console.WriteLine(
297+
"An error was thrown from the Concat method."
298+
);
299+
}
300+
}
301+
return count == 0;
302+
}
303+
}

source/projects/TextFileHelper/description.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ This is the first demo file
7979
*****
8080
* Testing Balanced
8181
*****
82-
0 = (0 = )43 = (47 = )0 = (30 = )ex0 is balanced: False
82+
ex0 is balanced: False
8383
ex1 is balanced: True
8484
ex2 is balanced: False
8585
ex3 is balanced: True
86-
8786
```
8887

8988
Note that it is ok if you cannot reproduce this output *exactly*.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Solution
2+
3+
## Simplest Solution
4+
5+
A possible solution is shared [in this archive](./code/projects/TextFileHelperSolution.zip)

0 commit comments

Comments
 (0)