Skip to content

Commit 051af15

Browse files
committed
Assn5 - Prob1
Added test case Incorporated PrependPair helper method
1 parent f0f382f commit 051af15

File tree

1 file changed

+78
-43
lines changed

1 file changed

+78
-43
lines changed

Assignment5/Problem1.cs

+78-43
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public static void RunTests()
3838
InputIntArray = new int[]{ 4 },
3939
CorrectNGEString = "{ {4, -1} }",
4040
},
41+
new TestCase
42+
{
43+
InputIntArray = new int[]{ 12, 8, 2, 20, 10, 5, 3, -4 },
44+
CorrectNGEString = "{ {12, 20}, {8, 20}, {2, 20}, {20, -1}, {10, -1}, {5, -1}, {3, -1}, {-4, -1} }",
45+
},
4146
//new TestCase
4247
//{
4348
// InputIntArray = new int[]{ 12, 9, 20, 10, 8, 0, 6, 9, 5, 10, 11},
@@ -148,36 +153,49 @@ public static string PrintElementsWithNextGreaterElements(int[] arr)
148153
if (arr.Length == 0)
149154
return "{}";
150155

151-
if (arr.Length == 1)
152-
return $"{{ {{{arr[0]}, {DEFAULT_NGE}}} }}";
153-
// For an interpolated string, use "{{" for a single '{' and "}}" for a single '}'
156+
var builder = new StringBuilder(" }");
157+
// Using the helper here instead of writing out the pattern multiple times
158+
// to be consistent
159+
PrependPair(arr[^1], DEFAULT_NGE, builder);
160+
// The last element, which may also be the first, and if it is:
154161

155-
// Remember that the Array.Clone method returns an object[], must cast it to appropriate type
156-
var arrNGE = (int[])arr.Clone();
162+
// Main logic requires array length of at least two
163+
if (arr.Length == 1)
164+
{
165+
builder[0] = '{';
166+
return builder.ToString();
167+
}
168+
//// From back when we were using a paralell array to store NGEs
169+
//// Remember that the Array.Clone method returns an object[], must cast it to appropriate type
170+
//var arrNGE = (int[])arr.Clone();
171+
//// By definition
172+
//arrNGE[^1] = DEFAULT_NGE;
157173

158-
var localMaxes = new Stack<int>();
159-
// remains empty until the right element of a pair is actually larger than the left element of a pair
160174

161-
// By definition
162-
arrNGE[^1] = DEFAULT_NGE;
175+
var localMaxes = new Stack<int>();
176+
// remains empty until the right element of a pair is actually larger than the left element of a pair
163177

164-
// Will refer to result of last comparison to determine if arr[i + 1] is determined to be a new local maximum
165-
var lastResult = CompResult.NoNotLessThan;
178+
// Will refer to result of last comparison to determine if arr[i + 1] is determined to be a new local maximum
179+
var lastResult = CompResult.NoNotLessThan;
166180
// NoNotLessThan is correct for the "comparison" between arr[^1] and the non-element to its right, since arr[^1] will be a local max if arr[i] < arr[i + 1]
167181
// Default is reserved to essentially mean uninitialized
168182

169183
var thisResult = CompResult.Default;
170184

171-
// walk array from right to left
172-
// i starts at next-to-last index
173-
// compare the elements in pairs
174-
for (var i = arr.Length - 2; i >= 0; --i)
185+
186+
// walk array from right to left
187+
// i starts at next-to-last index
188+
// compare the elements in pairs
189+
for (var i = arr.Length - 2; i >= 0; --i)
175190
{
176191
if (arr[i] < arr[i + 1])
177192
// Immediately adjacent element is NGE
178193
{
179194
thisResult = CompResult.YesLessThan;
180-
arrNGE[i] = arr[i + 1];
195+
PrependPair(arr[i], arr[i + 1], builder);
196+
197+
// From back when we were using a paralell array to store NGEs
198+
//arrNGE[i] = arr[i + 1];
181199

182200
if (lastResult == CompResult.NoNotLessThan)
183201
{
@@ -195,7 +213,11 @@ public static string PrintElementsWithNextGreaterElements(int[] arr)
195213
{
196214
if (arr[i] < localMaxes.Peek())
197215
{
198-
arrNGE[i] = localMaxes.Peek();
216+
PrependPair(arr[i], localMaxes.Peek(), builder);
217+
218+
// From back when we were using a paralell array to store NGEs
219+
//arrNGE[i] = localMaxes.Peek();
220+
199221
// Leave the NGE that we just used where we found it in localMaxes, it is still a candidate to be an NGE again in the future
200222
foundNGE = true;
201223
}
@@ -209,46 +231,52 @@ public static string PrintElementsWithNextGreaterElements(int[] arr)
209231
if (localMaxes.Count == 0)
210232
// No candidate NGEs exist, or arr[i] was greater than each candidate NGE
211233
{
212-
arrNGE[i] = DEFAULT_NGE;
213-
}
234+
PrependPair(arr[i], DEFAULT_NGE, builder);
214235

215-
// Not pushing onto localMaxes here, since we won't know until next iteration whether the current arr[i] is a local max
216-
}
236+
// From back when we were using a paralell array to store NGEs
237+
//arrNGE[i] = DEFAULT_NGE;
238+
}
239+
240+
// Not pushing onto localMaxes here, since we won't know until next iteration whether the current arr[i] is a local max
241+
}
217242

218243
// Advance state of tracking for next loop iteration
219244
lastResult = thisResult;
220245
thisResult = CompResult.Default;
221246
}
222247

223-
// Now construct the string left to right (or print it out as you go...but not since we're doing fixup at the end of the string building...and we're headed back to build the string as we go in the next version anyway)
248+
// Now construct the string left to right (or print it out as you go...but not since we're doing fixup at the end of the string building...and we're headed back to build the string as we go in the next version anyway)
224249

225-
// Example: { {4, 5}, {5, 25}, {2, 25}, {25, -1} }
250+
// Example: { {4, 5}, {5, 25}, {2, 25}, {25, -1} }
226251

227252
// TODO: Tell the builder how big it needs to be to avoid later reallocation
228253
// (Since we already know)
229-
var builder = new StringBuilder("{ ");
230254

231255

232-
// BUG: it's either k < arr.Length OR k <= arr.Length-1
233-
for (var k = 0; k < arr.Length; ++k)
234-
{
235-
// It's Append, not Add, for StringBuilder, right?
236-
builder.Append($"{{{arr[k]}, {arrNGE[k]}}}, ");
237-
// BUG: array index var for this loop is k, not i
238-
// But this loop is going away in the next version anyway
239-
}
256+
// From back when we were using a paralell array to store NGEs
257+
// // BUG: it's either k < arr.Length OR k <= arr.Length-1
258+
//for (var k = 0; k < arr.Length; ++k)
259+
//{
260+
// // It's Append, not Add, for StringBuilder, right?
261+
// builder.Append($"{{{arr[k]}, {arrNGE[k]}}}, ");
262+
// // BUG: array index var for this loop is k, not i
263+
// // But this loop is going away in the next version anyway
264+
//}
265+
266+
// // Drop the last comma
267+
// // param1: remove starting from what index
268+
// // param2: how many to remove
269+
// // that is: builder.Remove(where, howmany);
270+
// builder.Remove(builder.Length - 2, 1);
240271

241-
// Drop the last comma
242-
// param1: remove starting from what index
243-
// param2: how many to remove
244-
// that is: builder.Remove(where, howmany);
245-
builder.Remove(builder.Length - 2, 1);
272+
//// Add the final curly brace
273+
//builder.Append('}');
246274

247-
// Add the final curly brace
248-
builder.Append('}');
249275

276+
// Overwrite the final leading comma inserted by PrependPair
277+
builder[0] = '{';
250278

251-
return builder.ToString();
279+
return builder.ToString();
252280

253281

254282
// Go ahead and finish writing this, test that this solution works, then tomorrow adapt it to build the string right to left during initial traversal of the array (instead of cloning the array)
@@ -261,19 +289,26 @@ public static string PrintElementsWithNextGreaterElements(int[] arr)
261289
}
262290

263291

264-
private void PrependPair(int ele, int nge, StringBuilder sb)
292+
// Thought about telling StringBuilder how big to be,
293+
// but that depends on how many digits each key and value
294+
// ends up being.
295+
// Let's leave out this complexity for now.
296+
297+
private static void PrependPair(int ele, int nge, StringBuilder sb)
265298
{
266299
// Example of overall finished string:
267300
// "{ {4, 5}, {5, 25}, {2, 25}, {25, -1} }"
268301

269302
// Each call adds this much:
270-
// ", {25, -1} "
303+
// ", {25, -1}"
271304

272305
// param1: insert starting at what index
273306
// param2: what to insert
274307
// that is: sb.Insert(where, what);
275308

276-
sb.Insert(0, $", {{{ele}, {nge}}} ");
309+
// For an interpolated string, use "{{" for a single '{' and "}}" for a single '}'
310+
311+
sb.Insert(0, $", {{{ele}, {nge}}}");
277312

278313

279314
// Character array would be more performant

0 commit comments

Comments
 (0)