Skip to content

Commit ada1b92

Browse files
committed
Fixed missing namespace using for Utilities methods
1 parent 3d12a89 commit ada1b92

File tree

12 files changed

+250
-21
lines changed

12 files changed

+250
-21
lines changed

Assignment4/Problem1.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using DataStructuresAndAlgos;
2+
using System;
23
using System.Collections.Generic;
34

45
namespace Assignment4

Assignment4/Problem2.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using DataStructuresAndAlgos;
2+
using System;
23
using System.Collections.Generic;
34

45
namespace Assignment4

Assignment4/Problem3.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using DataStructuresAndAlgos;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

Assignment4/Problem4.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using DataStructuresAndAlgos;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

Assignment4/Problem5.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using DataStructuresAndAlgos;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq; // For sequence equal
45
using System.Text;

Assignment4/Problem6.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using DataStructuresAndAlgos;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

Assignment4/Problem7.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using DataStructuresAndAlgos;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

Assignment4/Problem8.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44
using System.Linq;
5+
using DataStructuresAndAlgos;
56

67
namespace Assignment4
78
{

Assignment4/Problem9.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using DataStructuresAndAlgos;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

Assignment5/Problem1.cs

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
using DataStructuresAndAlgos;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Assignment5
7+
{
8+
class Problem1
9+
{
10+
public static void RunTests()
11+
{
12+
var testCases = new List<TestCase>
13+
{
14+
new TestCase
15+
{
16+
InputIntArray = new int[]{ 4, 5, 2, 25 },
17+
CorrectLeadersString = "{ {4, 5}, {5, 25}, {2, 25}, {25, -1} }",
18+
},
19+
new TestCase
20+
{
21+
InputIntArray = new int[]{ 13, 7, 6, 12 },
22+
CorrectLeadersString = "{ {13, -1}, {7, 12}, {6, 12}, {12, -1} }",
23+
},
24+
//new TestCase
25+
//{
26+
// InputIntArray = new int[]{ 12, 9, 20, 10, 8, 0, 6, 9, 5, 10, 11},
27+
// CorrectLeadersString = $"leaders are 20 and 11",
28+
//},
29+
//new TestCase
30+
//{
31+
// InputIntArray = new int[]{ 0, -6, -10, -12, -20, -8, -5},
32+
// CorrectLeadersString = $"leaders are 0 and -5",
33+
//},
34+
//new TestCase
35+
//{
36+
// InputIntArray = new int[]{ -20, -5, -20, -10, -5, -10},
37+
// CorrectLeadersString = $"leaders are -5 and -10",
38+
//},
39+
//new TestCase
40+
//{
41+
// InputIntArray = new int[]{0},
42+
// CorrectLeadersString = $"leader is 0",
43+
//},
44+
//new TestCase
45+
//{
46+
// InputIntArray = new int[]{0, 1, 2, 3, 4, 5, 6},
47+
// CorrectLeadersString = $"leader is 6",
48+
//},
49+
//new TestCase
50+
//{
51+
// InputIntArray = new int[]{ 4, 3, 1, 2},
52+
// CorrectLeadersString = $"leaders are 4, 3 and 2",
53+
//},
54+
};
55+
56+
57+
string intro =
58+
"==============\n" +
59+
"= Problem #7 =\n" +
60+
"==============\n" +
61+
"\n" +
62+
"Write a program to print all the LEADERS in the array." +
63+
"An element is leader if it is greater than all the elements to its right side." +
64+
"And the rightmost element is always a leader." +
65+
"For example int the array { 16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2.";
66+
67+
Console.WriteLine(intro);
68+
69+
int testOopsCount = 0;
70+
71+
for (var i = 0; i < testCases.Count; ++i)
72+
{
73+
Console.WriteLine($"\nTest #{i + 1}:");
74+
75+
76+
Console.WriteLine($"For the array: {Utility.CollectionToString(testCases[i].InputIntArray)}");
77+
Console.WriteLine($"The correct result is: {testCases[i].CorrectLeadersString}.");
78+
79+
var testCaseResult = PrintLeadersToString(testCases[i].InputIntArray);
80+
81+
string resultMessage;
82+
83+
if (testCaseResult == testCases[i].CorrectLeadersString)
84+
{
85+
resultMessage = "SUCCESS";
86+
}
87+
else
88+
{
89+
++testOopsCount;
90+
resultMessage = "OOPS";
91+
}
92+
93+
Console.WriteLine($"{resultMessage}! Your answer is: {testCaseResult}.");
94+
}
95+
96+
var testCount = testCases.Count;
97+
var testSuccessCount = testCount - testOopsCount;
98+
99+
Console.WriteLine($"\n\nOut of {testCount} tests total,\n");
100+
Console.WriteLine($"{testSuccessCount}/{testCount} tests succeeded, and");
101+
Console.WriteLine($"{testOopsCount}/{testCount} tests oopsed.\n");
102+
103+
if (testOopsCount == 0)
104+
{
105+
Console.WriteLine($"YAY! All tests succeeded! :D\n");
106+
}
107+
}
108+
109+
110+
public static string PrintLeadersToString(int[] arr)
111+
{
112+
if (arr == null)
113+
throw new ArgumentNullException("The int[] arr parameter is null.");
114+
115+
if (arr.Length == 0)
116+
throw new ArgumentException("The parameter int[] arr is empty.");
117+
118+
var largestLeader = arr[arr.Length - 1];
119+
120+
//var leaders = new Queue<int> { largestLeader }; // Can you init a queue in this way? nope.
121+
var leaders = new Queue<int>();
122+
leaders.Enqueue(largestLeader);
123+
124+
// Walk through arr right to left, starting at the the element to the left of the default leader
125+
for (var i = arr.Length - 2; i >= 0; --i)
126+
{
127+
//if (arr[i] > arr[i + 1]) // No, don't compare to the element to the right
128+
// Compare to the largestLeader
129+
if (arr[i] > largestLeader)
130+
{
131+
largestLeader = arr[i];
132+
leaders.Enqueue(largestLeader);
133+
}
134+
}
135+
136+
return ConstructLeaderResultString(leaders);
137+
}
138+
139+
140+
private static string ConstructLeaderResultString(Queue<int> leaders)
141+
{
142+
// Example: "leaders are 17, 5 and 2"
143+
144+
if (leaders.Count == 0)
145+
throw new ArgumentException("Queue<int> leaders parameter is empty.");
146+
147+
if (leaders.Count == 1)
148+
return $"leader is {leaders.Dequeue()}";
149+
150+
151+
// Build from the right of the string to the left
152+
var builder = new StringBuilder($" and {leaders.Dequeue()}");
153+
154+
// Insert at 0, or prepend, whatever the method is for that
155+
builder.Insert(0, leaders.Dequeue());
156+
157+
// When you Insert into a StringBuilder (probably onto the front of it,
158+
// as a prepend), the index (probably zero) is the first param, while
159+
// the string or thing-to-be-stringified is the second param
160+
161+
162+
while (leaders.Count > 0)
163+
{
164+
builder.Insert(0, $"{leaders.Dequeue()}, ");
165+
}
166+
167+
builder.Insert(0, "leaders are ");
168+
169+
return builder.ToString();
170+
}
171+
172+
173+
// made for video
174+
public static void PrintLeaders(int[] arr)
175+
{
176+
if (arr == null)
177+
throw new ArgumentNullException("The int[] arr parameter is null.");
178+
179+
if (arr.Length == 0)
180+
throw new ArgumentException("The parameter int[] arr is empty.");
181+
182+
var largestLeader = arr[arr.Length - 1];
183+
184+
var leaders = new Queue<int>();
185+
leaders.Enqueue(largestLeader);
186+
187+
// Walk through arr right to left,
188+
// starting at the the element to the left of the default leader
189+
for (var i = arr.Length - 2; i >= 0; --i)
190+
{
191+
// Compare to the largestLeader
192+
if (arr[i] > largestLeader)
193+
{
194+
largestLeader = arr[i];
195+
leaders.Enqueue(largestLeader);
196+
}
197+
}
198+
199+
// Example: "leaders are 17, 5 and 2"
200+
if (leaders.Count == 1)
201+
Console.WriteLine($"leader is {leaders.Dequeue()}");
202+
else
203+
{
204+
// Build from the right of the string to the left
205+
var builder = new StringBuilder($" and {leaders.Dequeue()}");
206+
207+
// Use Insert at 0 as prepend
208+
builder.Insert(0, leaders.Dequeue());
209+
210+
while (leaders.Count > 0)
211+
{
212+
builder.Insert(0, $"{leaders.Dequeue()}, ");
213+
}
214+
215+
builder.Insert(0, "leaders are ");
216+
217+
Console.WriteLine(builder.ToString());
218+
}
219+
}
220+
221+
222+
private class TestCase
223+
{
224+
public int[] InputIntArray { get; set; }
225+
226+
public string CorrectLeadersString { get; set; }
227+
}
228+
}
229+
}

0 commit comments

Comments
 (0)