Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 049fed6

Browse files
committed
Small refactoring
1 parent e2ba56e commit 049fed6

File tree

1 file changed

+64
-85
lines changed
  • CodingInterview/CodingInterview

1 file changed

+64
-85
lines changed

‎CodingInterview/CodingInterview/Tests.cs

Lines changed: 64 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,34 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Text;
65

76
namespace CodingInterview
87
{
98
public class Tests
109
{
11-
int[] RemoveDuplicates(int[] arrayOfNumbers)
10+
privatestaticint[] RemoveDuplicates(IReadOnlyList<int> arrayOfNumbers)
1211
{
1312
var numbers = new HashSet<int>();
14-
for(intindex=0;index<arrayOfNumbers.Length;index++)
13+
foreach(varelementinarrayOfNumbers)
1514
{
16-
numbers.Add(arrayOfNumbers[index]);
15+
numbers.Add(element);
1716
}
1817
return numbers.ToArray();
1918
}
2019

21-
int[] LeaveTwoDuplicates(int[] arrayOfNumbers)
20+
privatestaticint[] LeaveTwoDuplicates(IReadOnlyCollection<int> arrayOfNumbers)
2221
{
2322
var numbers = new HashSet<int>();
2423
var numbersWithTwoDuplicates = new List<int>();
25-
for(intindex=0;index<arrayOfNumbers.Length;index++)
24+
foreach(varelementinarrayOfNumbers)
2625
{
27-
numbers.Add(arrayOfNumbers[index]);
26+
numbers.Add(element);
2827
}
2928

30-
for (int index = 0; index < numbers.Count; index++)
29+
for (var index = 0; index < numbers.Count; index++)
3130
{
3231
var actualValue = numbers.ElementAt(index);
33-
if (arrayOfNumbers.Where(element => element == actualValue).Count() >= 2)
32+
if (arrayOfNumbers.Count(element => element == actualValue) >= 2)
3433
{
3534
numbersWithTwoDuplicates.Add(actualValue);
3635
}
@@ -39,20 +38,19 @@ int[] LeaveTwoDuplicates(int[] arrayOfNumbers)
3938
return numbersWithTwoDuplicates.ToArray();
4039
}
4140

42-
int[] RemoveElement(int[] arrayOfNumbers, int elementToRemove)
41+
privatestaticint[] RemoveElement(IReadOnlyCollection<int> arrayOfNumbers, int elementToRemove)
4342
{
4443
return arrayOfNumbers
4544
.Where(element => element != elementToRemove)
4645
.ToArray();
4746
}
4847

49-
int[] MoveZeros(int[] arrayOfNumbers)
48+
privatestaticint[] MoveZeros(IReadOnlyList<int> arrayOfNumbers)
5049
{
5150
var zeroCount = 0;
5251
var listOfElements = new List<int>();
53-
for(intindex=0;index<arrayOfNumbers.Length;index++)
52+
foreach(varactualElementinarrayOfNumbers)
5453
{
55-
var actualElement = arrayOfNumbers[index];
5654
if (actualElement == 0)
5755
{
5856
zeroCount++;
@@ -68,28 +66,25 @@ int[] MoveZeros(int[] arrayOfNumbers)
6866
return listOfElements.ToArray();
6967
}
7068

71-
int[] GetProductofArrayExceptSelf(int[] inputArray)
69+
privatestaticint[] GetProductofArrayExceptSelf(IReadOnlyList<int> inputArray)
7270
{
73-
if (inputArray == null || inputArray.Length == 0)
71+
if (inputArray == null || inputArray.Count == 0)
7472
throw new ArgumentNullException("inputArray needs to have elements");
75-
var outputArray = new List<int>();
76-
for (int index = 0; index < inputArray.Length; index++)
77-
{
78-
outputArray.Add(inputArray
79-
.Where(element => element != inputArray[index])
80-
.Aggregate((firstElement, secondElement) => firstElement * secondElement));
81-
}
82-
return outputArray.ToArray();
73+
74+
return inputArray
75+
.Select(arrayElement => inputArray.Where(element => element != arrayElement)
76+
.Aggregate((firstElement, secondElement) => firstElement * secondElement))
77+
.ToArray();
8378
}
8479

85-
int[] GetMinimumSizeSubarraySum(int[] inputArray, int n)
80+
privatestaticint[] GetMinimumSizeSubarraySum(IReadOnlyList<int> inputArray, int n)
8681
{
8782
var listOfSums = new List<int>();
8883
var listOfNumbers = new List<int>();
8984
var sum = 0;
90-
for (int subArraySize = 2; subArraySize <= inputArray.Length - 1; subArraySize++)
85+
for (var subArraySize = 2; subArraySize <= inputArray.Count - 1; subArraySize++)
9186
{
92-
for (int index = 0; index <= inputArray.Length - 1; index++)
87+
for (var index = 0; index <= inputArray.Count - 1; index++)
9388
{
9489
sum += inputArray[index];
9590
listOfNumbers.Add(inputArray[index]);
@@ -104,39 +99,38 @@ int[] GetMinimumSizeSubarraySum(int[] inputArray, int n)
10499
listOfSums.Add(sum);
105100
sum = 0;
106101
listOfNumbers.Clear();
107-
continue;
108102
}
109103
}
110104
}
111105
return null;
112106
}
113107

114-
string[] GetSummaryRanges(int[] inputArray)
108+
privatestaticstring[] GetSummaryRanges(int[] inputArray)
115109
{
116110
if (inputArray == null || inputArray.Length == 0)
117111
throw new ArgumentNullException("inputArray needs to have elements");
118112

119113
var listOfAnswers = new List<string>();
120114

121-
for (int index = 0; index < inputArray.Length; index++)
115+
for (var index = 0; index < inputArray.Length; index++)
122116
{
123117
var firstString = string.Empty;
124-
boolfound = false;
125-
for (int insideIndex = index; insideIndex < inputArray.Length; insideIndex++)
118+
varisFounded = false;
119+
for (var insideIndex = index; insideIndex < inputArray.Length; insideIndex++)
126120
{
127121
if (insideIndex + 1 < inputArray.Length && (inputArray[insideIndex + 1] - inputArray[insideIndex] == 1))
128122
{
129-
if (!found)
123+
if (!isFounded)
130124
{
131125
firstString = $"{inputArray[insideIndex]}";
132126
}
133-
found = true;
127+
isFounded = true;
134128
}
135129
else
136130
{
137-
if (found)
131+
if (isFounded)
138132
{
139-
string endString = inputArray[insideIndex].ToString();
133+
var endString = inputArray[insideIndex].ToString();
140134
listOfAnswers.Add($"{firstString}->{endString}");
141135
index = insideIndex;
142136
break;
@@ -151,43 +145,26 @@ string[] GetSummaryRanges(int[] inputArray)
151145
return listOfAnswers.ToArray();
152146
}
153147

154-
string[] GetMissingRanges(int lower, int upper, int[] inputArray)
148+
privatestaticstring[] GetMissingRanges(int lower, int upper, IReadOnlyCollection<int> inputArray)
155149
{
156-
if (inputArray == null || inputArray.Length == 0)
150+
if (inputArray == null || inputArray.Count == 0)
157151
throw new ArgumentNullException("InputArray needs to have elements");
158152

159153
if (lower > upper && lower >= 0 && upper >= 0)
160154
throw new ArgumentException("Correct arguments");
161155

162156
var negativeList = Enumerable.Range(lower, upper - lower + 1)
163-
.Where(element => !inputArray.Any(item => item == element));
157+
.Where(element => inputArray.All(item => item != element));
164158

165159
return GetSummaryRanges(negativeList.ToArray());
166160
}
167161

168-
int[][] GetMergeIntervals(int[][]inputArray)
162+
privatestaticint[][] GetMergeIntervals()
169163
{
170-
//given
171-
//var inputArray = new int[][] {
172-
// new int[] { 1, 3 },
173-
// new int[] { 2, 6 },
174-
// new int[] { 8, 10 },
175-
// new int[] { 15, 18 }
176-
//};
177-
//var expectedArray = new int[][] {
178-
// new int[] { 1, 6 },
179-
// new int[] { 8, 10 },
180-
// new int[] { 15, 18 }
181-
//};
182-
183-
for (var index = 1; index < inputArray.Length; index += 2)
184-
{
185-
186-
}
187-
return inputArray;
164+
return null;
188165
}
189166

190-
public bool CheckIfStringIsPalindrome(string message)
167+
privatestatic bool CheckIfStringIsPalindrome(string message)
191168
{
192169
if (string.IsNullOrEmpty(message))
193170
return false;
@@ -205,16 +182,16 @@ public bool CheckIfStringIsPalindrome(string message)
205182
private static string GetAlphabeticString(string message)
206183
{
207184
var alphabeticMessageArray = message.ToCharArray().Where(element => (element >= 'a' && element <= 'z') || (element >= 'A' && element <= 'Z'));
208-
string alphabeticMessageString = string.Join("", alphabeticMessageArray).ToLower();
185+
var alphabeticMessageString = string.Join("", alphabeticMessageArray).ToLower();
209186
return alphabeticMessageString;
210187
}
211188

212189
[Test]
213190
public void RemoveDuplicatesFromSortedArray_1()
214191
{
215192
//given
216-
var sortedArrayWithDuplicates = new int[] { 1, 3, 3, 6, 8, 8, 9 };
217-
var expectedSortedArrayWithoutDuplicates = new int[] { 1, 3, 6, 8, 9 };
193+
var sortedArrayWithDuplicates = new [] { 1, 3, 3, 6, 8, 8, 9 };
194+
var expectedSortedArrayWithoutDuplicates = new [] { 1, 3, 6, 8, 9 };
218195
//when
219196
var sortedArrayWithoutDuplicatesCalculated = RemoveDuplicates(sortedArrayWithDuplicates);
220197
//then
@@ -225,8 +202,8 @@ public void RemoveDuplicatesFromSortedArray_1()
225202
public void RemoveDuplicatesFromSortedArray_2()
226203
{
227204
//given
228-
var sortedArrayWithDuplicates = new int[] { 1, 3, 3, 3, 6, 8, 8, 9, 9, 9 };
229-
var expectedSortedArrayWithTwoDuplicates = new int[] { 1, 3, 3, 6, 8, 8, 9, 9 };
205+
var sortedArrayWithDuplicates = new [] { 1, 3, 3, 3, 6, 8, 8, 9, 9, 9 };
206+
var expectedSortedArrayWithTwoDuplicates = new [] { 1, 3, 3, 6, 8, 8, 9, 9 };
230207
//when
231208
var sortedArrayWithTwoDuplicatesCalculated = LeaveTwoDuplicates(sortedArrayWithDuplicates);
232209
//then
@@ -237,8 +214,8 @@ public void RemoveDuplicatesFromSortedArray_2()
237214
public void RemoveElement_3()
238215
{
239216
//given
240-
var arrayWithElements = new int[] { 1, 5, 3, 3, 6, 1, 8, 3, 9, 7 };
241-
var expectedArrayWithElements = new int[] { 1, 5, 6, 1, 8, 9, 7 };
217+
var arrayWithElements = new [] { 1, 5, 3, 3, 6, 1, 8, 3, 9, 7 };
218+
var expectedArrayWithElements = new [] { 1, 5, 6, 1, 8, 9, 7 };
242219
var elementToRemove = 3;
243220
//when
244221
var arrayWithRemoveElement = RemoveElement(arrayWithElements, elementToRemove);
@@ -250,8 +227,8 @@ public void RemoveElement_3()
250227
public void MoveZeros_4()
251228
{
252229
//given
253-
var arrayWithZeros = new int[] { 1, 0, 5, 0, 0, 3 };
254-
var expectedArrayWithZerosInTheEnd = new int[] { 1, 5, 3, 0, 0, 0 };
230+
var arrayWithZeros = new [] { 1, 0, 5, 0, 0, 3 };
231+
var expectedArrayWithZerosInTheEnd = new [] { 1, 5, 3, 0, 0, 0 };
255232
//when
256233
var arrayWithRemoveElement = MoveZeros(arrayWithZeros);
257234
//then
@@ -262,8 +239,8 @@ public void MoveZeros_4()
262239
public void ProductofArrayExceptSelf_7()
263240
{
264241
//given
265-
var inputArray = new int[] { 1, 2, 3, 4 };
266-
var expectedArray = new int[] { 24, 12, 8, 6 };
242+
var inputArray = new [] { 1, 2, 3, 4 };
243+
var expectedArray = new [] { 24, 12, 8, 6 };
267244
//when
268245
var outputArray = GetProductofArrayExceptSelf(inputArray);
269246
//then
@@ -274,9 +251,9 @@ public void ProductofArrayExceptSelf_7()
274251
public void MinimumSizeSubarraySum_8()
275252
{
276253
//given
277-
var inputArray = new int[] { 2, 3, 1, 2, 4, 3 };
254+
var inputArray = new [] { 2, 3, 1, 2, 4, 3 };
278255
int n = 7;
279-
var expectedArray = new int[] { 4, 3 };
256+
var expectedArray = new [] { 4, 3 };
280257
//when
281258
var outputArray = GetMinimumSizeSubarraySum(inputArray, n);
282259
//then
@@ -287,8 +264,8 @@ public void MinimumSizeSubarraySum_8()
287264
public void SummaryRanges_9()
288265
{
289266
//given
290-
var inputArray = new int[] { 0, 1, 2, 4, 5, 7 };
291-
var expectedArray = new string[] { "0->2", "4->5", "7" };
267+
var inputArray = new [] { 0, 1, 2, 4, 5, 7 };
268+
var expectedArray = new [] { "0->2", "4->5", "7" };
292269
//when
293270
var outputArray = GetSummaryRanges(inputArray);
294271
//then
@@ -299,10 +276,10 @@ public void SummaryRanges_9()
299276
public void MissingRanges_10()
300277
{
301278
//given
302-
var inputArray = new int[] { 0, 1, 3, 50, 75 };
279+
var inputArray = new [] { 0, 1, 3, 50, 75 };
303280
int lower = 0;
304281
int upper = 99;
305-
var expectedArray = new string[] { "2", "4->49", "51->74", "76->99" };
282+
var expectedArray = new [] { "2", "4->49", "51->74", "76->99" };
306283
//when
307284
var outputArray = GetMissingRanges(lower, upper, inputArray);
308285
//then
@@ -313,19 +290,21 @@ public void MissingRanges_10()
313290
public void MergeIntervals_11()
314291
{
315292
//given
316-
var inputArray = new int[][] {
317-
new int[] { 1, 3 },
318-
new int[] { 2, 6 },
319-
new int[] { 8, 10 },
320-
new int[] { 15, 18 }
293+
var inputArray = new[]
294+
{
295+
new [] { 1, 3 },
296+
new [] { 2, 6 },
297+
new [] { 8, 10 },
298+
new [] { 15, 18 }
321299
};
322-
var expectedArray = new int[][] {
323-
new int[] { 1, 6 },
324-
new int[] { 8, 10 },
325-
new int[] { 15, 18 }
300+
var expectedArray = new[]
301+
{
302+
new [] { 1, 6 },
303+
new [] { 8, 10 },
304+
new [] { 15, 18 }
326305
};
327306
//when
328-
var outputArray = GetMergeIntervals(inputArray);
307+
var outputArray = GetMergeIntervals();
329308
//then
330309
Assert.AreEqual(expectedArray, outputArray);
331310
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /