Skip to content

Commit 831de54

Browse files
author
Gonzalo Diaz
committed
WIP
1 parent 722a079 commit 831de54

File tree

10 files changed

+214
-80
lines changed

10 files changed

+214
-80
lines changed

src/algorithm_exercises_csharp/hackerrank/warmup/AVeryBigSum.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22

33
namespace algorithm_exercises_csharp.hackerrank.warmup;
44

5-
using System.Diagnostics.CodeAnalysis;
6-
7-
public class AVeryBigSum
5+
public static class AVeryBigSum
86
{
9-
[ExcludeFromCodeCoverage]
10-
protected AVeryBigSum() { }
11-
12-
public static long aVeryBigSum(List<long> _ar)
7+
public static long aVeryBigSum(List<long> ar)
138
{
9+
ArgumentNullException.ThrowIfNull(ar);
10+
1411
var total = 0L;
1512

16-
foreach (long x in _ar)
13+
foreach (long x in ar)
1714
{
1815
total += x;
1916
}

src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.Test.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ public class ArraysLeftRotationTest
77
{
88
public class ArraysLeftRotationsTestCase
99
{
10-
public List<int> input { get; } = default!;
10+
public List<int> input { get; set; } = default!;
1111
public int d { get; set; }
12-
public List<int> expected { get; } = default!;
12+
public List<int> expected { get; set; } = default!;
1313
}
1414

1515
private List<ArraysLeftRotationsTestCase> testCases = default!;

src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/arrays/CrushBruteForce.Test.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class CrushBruteForceTest
88
public class CrushBruteForceTestCase
99
{
1010
public string title { get; set; } = default!;
11-
public List<List<int>> queries { get; } = default!;
11+
public List<List<int>> queries { get; set; } = default!;
1212
public int n { get; set; } = default!;
1313
public long expected { get; set; } = default!;
1414
}

src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/FrequencyQueries.Test.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public class FrequencyQueriesTest
88
public class FrequencyQueriesTestCase
99
{
1010
public string title { get; set; } = default!;
11-
public List<List<int>> input { get; } = default!;
12-
public List<int> expected { get; } = default!;
11+
public List<List<int>> input { get; set; } = default!;
12+
public List<int> expected { get; set; } = default!;
1313
}
1414

1515
List<FrequencyQueriesTestCase> testCases { get; set; } = default!;

src/algorithm_exercises_csharp_test/hackerrank/warmup/AVeryBigSumTest.cs

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
11
namespace algorithm_exercises_csharp_test.hackerrank.warmup;
22
using algorithm_exercises_csharp.hackerrank.warmup;
33

4+
using algorithm_exercises_csharp_test.lib;
5+
46
using Microsoft.VisualStudio.TestTools.UnitTesting;
57

68
[TestClass]
79
public class AVeryBigSumTest
810
{
9-
public class AVeryBigSumTestCase
11+
public class AVeryBigSumTestCase(List<long> ar, long expected)
1012
{
11-
public List<long> Ar { get; set; } = default!;
12-
public long Expected { get; set; }
13-
}
13+
private readonly List<long> ar = ar;
14+
private readonly long expected = expected;
15+
16+
public List<long> Ar
17+
{
18+
get { return ar; }
19+
}
1420

15-
private static readonly AVeryBigSumTestCase[] tests = [
16-
new()
21+
public long Expected
1722
{
18-
Ar = [1000000001, 1000000002, 1000000003, 1000000004, 1000000005],
19-
Expected = 5000000015
23+
get { return expected; }
2024
}
21-
];
25+
}
26+
27+
private List<AVeryBigSumTestCase> testCases { get; set; } = default!;
28+
29+
[TestInitialize]
30+
public void testInitialize()
31+
{
32+
testCases = JsonLoader.stringLoad<List<AVeryBigSumTestCase>>(/*lang=json,strict*/ @"
33+
[
34+
{
35+
""ar"": [1000000001, 1000000002, 1000000003, 1000000004, 1000000005],
36+
""expected"": 5000000015
37+
}
38+
]
39+
") ?? [];
40+
}
2241

2342
[TestMethod]
2443
public void testSimpleArraySum()
2544
{
2645
long result;
2746

28-
foreach (AVeryBigSumTestCase test in tests)
47+
foreach (AVeryBigSumTestCase test in testCases)
2948
{
3049
result = AVeryBigSum.aVeryBigSum(test.Ar);
3150
Assert.AreEqual(test.Expected, result);

src/algorithm_exercises_csharp_test/hackerrank/warmup/BirthdayCakeCandles.Test.cs

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
11
namespace algorithm_exercises_csharp_test.hackerrank.warmup;
22
using algorithm_exercises_csharp.hackerrank.warmup;
33

4+
using algorithm_exercises_csharp_test.lib;
5+
46
[TestClass]
57
public class BirthdayCakeCandlesTest
68
{
7-
public class BirthdayCakeCandlesTestCase
9+
public class BirthdayCakeCandlesTestCase(List<int> input, long expected)
810
{
9-
public List<int> input = [];
10-
public int expected = 0;
11+
private readonly List<int> input = input;
12+
private readonly long expected = expected;
13+
14+
public List<int> Input
15+
{
16+
get { return input; }
17+
}
18+
19+
public long Expected
20+
{
21+
get { return expected; }
22+
}
1123
}
1224

13-
private static readonly BirthdayCakeCandlesTestCase[] tests = [
14-
new() { input = [3, 2, 1, 3], expected = 2 },
15-
new() { input = [1, 2, 3, 3], expected = 2 }
16-
];
25+
private List<BirthdayCakeCandlesTestCase> testCases { get; set; } = default!;
26+
27+
[TestInitialize]
28+
public void testInitialize()
29+
{
30+
testCases = JsonLoader.stringLoad<List<BirthdayCakeCandlesTestCase>>(/*lang=json,strict*/ @"
31+
[
32+
{ ""input"": [3, 2, 1, 3], ""expected"": 2 },
33+
{ ""input"": [1, 2, 3, 3], ""expected"": 2 }
34+
]
35+
") ?? [];
36+
}
1737

1838
[TestMethod]
1939
public void testBirthdayCakeCandles()
2040
{
21-
int? result;
41+
long? result;
2242

23-
foreach (BirthdayCakeCandlesTestCase test in tests)
43+
foreach (BirthdayCakeCandlesTestCase test in testCases)
2444
{
25-
result = BirthdayCakeCandles.birthdayCakeCandles(test.input);
26-
Assert.AreEqual(test.expected, result);
45+
result = BirthdayCakeCandles.birthdayCakeCandles(test.Input);
46+
Assert.AreEqual(test.Expected, result);
2747
}
2848
}
2949

src/algorithm_exercises_csharp_test/hackerrank/warmup/CompareTriplets.Test.cs

+38-15
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,60 @@
11
namespace algorithm_exercises_csharp_test.hackerrank.warmup;
22
using algorithm_exercises_csharp.hackerrank.warmup;
33

4+
using algorithm_exercises_csharp_test.lib;
5+
46
using Microsoft.VisualStudio.TestTools.UnitTesting;
57

68
[TestClass]
79
public class CompareTripletsTest
810
{
9-
public class CompareTripletsTestCase
11+
public class CompareTripletsTestCase(List<int> a, List<int> b, List<int> expected)
1012
{
11-
public List<int> a = [];
12-
public List<int> b = [];
13-
public List<int> expected = [];
14-
}
13+
private readonly List<int> a = a;
14+
private readonly List<int> b = b;
15+
private readonly List<int> expected = expected;
1516

16-
// dotnet_style_readonly_field = true
17-
private static readonly CompareTripletsTestCase[] tests = [
18-
new()
17+
public List<int> A
1918
{
20-
a = [5, 6, 7],
21-
b = [3, 6, 10],
22-
expected = [1, 1]
19+
get { return a; }
2320
}
24-
];
21+
22+
public List<int> B
23+
{
24+
get { return b; }
25+
}
26+
27+
public List<int> Expected
28+
{
29+
get { return expected; }
30+
}
31+
}
32+
33+
private List<CompareTripletsTestCase> testCases { get; set; } = default!;
34+
35+
[TestInitialize]
36+
public void testInitialize()
37+
{
38+
testCases = JsonLoader.stringLoad<List<CompareTripletsTestCase>>(/*lang=json,strict*/ @"
39+
[
40+
{
41+
""a"": [5, 6, 7],
42+
""b"": [3, 6, 10],
43+
""expected"": [1, 1]
44+
}
45+
]
46+
") ?? [];
47+
}
2548

2649
[TestMethod]
2750
public void testSimpleArraySum()
2851
{
2952
List<int> result;
3053

31-
foreach (CompareTripletsTestCase test in tests)
54+
foreach (CompareTripletsTestCase test in testCases)
3255
{
33-
result = CompareTriplets.compareTriplets(test.a, test.b);
34-
CollectionAssert.AreEquivalent(test.expected, result);
56+
result = CompareTriplets.compareTriplets(test.A, test.B);
57+
CollectionAssert.AreEquivalent(test.Expected, result);
3558
}
3659
}
3760
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,58 @@
11
namespace algorithm_exercises_csharp_test.hackerrank.warmup;
22
using algorithm_exercises_csharp.hackerrank.warmup;
33

4+
using algorithm_exercises_csharp_test.lib;
5+
46
[TestClass]
57
public class DiagonalDifferenceTest
68
{
9+
public class DiagonalDifferenceTestCase(List<List<int>> arr, int expected)
10+
{
11+
private readonly List<List<int>> arr = arr;
12+
private readonly int expected = expected;
13+
14+
public List<List<int>> Arr
15+
{
16+
get { return arr; }
17+
}
18+
19+
public int Expected
20+
{
21+
get { return expected; }
22+
}
23+
}
24+
25+
private List<DiagonalDifferenceTestCase> testCases { get; set; } = default!;
26+
27+
[TestInitialize]
28+
public void testInitialize()
29+
{
30+
testCases = JsonLoader.stringLoad<List<DiagonalDifferenceTestCase>>(/*lang=json,strict*/ @"
31+
[
32+
{
33+
""arr"": [
34+
[11, 2, 4],
35+
[4, 5, 6],
36+
[10, 8, -12]
37+
],
38+
""expected"": 15
39+
}
40+
]
41+
") ?? [];
42+
}
43+
744
[TestMethod]
845
public void testDiagonalDifference()
946
{
10-
List<List<int>> arr = [
11-
[11, 2, 4],
12-
[4, 5, 6],
13-
[10, 8, -12]
14-
];
15-
int expectedAnswer = 15;
16-
int result = DiagonalDifference.diagonalDifference(arr);
17-
18-
Assert.AreEqual(expectedAnswer, result,
19-
String.Format("DiagonalDifference.diagonalDifference answer must be: {0}", expectedAnswer)
20-
);
47+
int? result;
48+
49+
foreach (DiagonalDifferenceTestCase test in testCases)
50+
{
51+
result = DiagonalDifference.diagonalDifference(test.Arr);
52+
Assert.AreEqual(test.Expected, result,
53+
string.Format(System.Globalization.CultureInfo.InvariantCulture, "DiagonalDifference.diagonalDifference answer must be: {0}", test.Expected)
54+
);
55+
}
2156
}
2257
}
2358

src/algorithm_exercises_csharp_test/hackerrank/warmup/MiniMaxSum.Test.cs

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
11
namespace algorithm_exercises_csharp_test.hackerrank.warmup;
22
using algorithm_exercises_csharp.hackerrank.warmup;
33

4+
using algorithm_exercises_csharp_test.lib;
5+
46
[TestClass]
57
public class MiniMaxSumTest
68
{
7-
public class MiniMaxSumTestCase
9+
public class MiniMaxSumTestCase(List<int> input, string expected)
810
{
9-
public List<int> input = [];
10-
public string expected = "";
11+
private readonly List<int> input = input;
12+
private readonly string expected = expected;
13+
14+
public List<int> Input
15+
{
16+
get { return input; }
17+
}
18+
19+
public string Expected
20+
{
21+
get { return expected; }
22+
}
1123
}
1224

13-
private static readonly MiniMaxSumTestCase[] tests = [
14-
new() { input = [1, 2, 3, 4, 5], expected = "10 14" },
15-
new() { input = [5, 4, 3, 2, 1], expected = "10 14" }
16-
];
25+
private List<MiniMaxSumTestCase> testCases { get; set; } = default!;
26+
27+
[TestInitialize]
28+
public void testInitialize()
29+
{
30+
testCases = JsonLoader.stringLoad<List<MiniMaxSumTestCase>>(/*lang=json,strict*/ @"
31+
[
32+
{ ""input"": [1, 2, 3, 4, 5], ""expected"": ""10 14"" },
33+
{ ""input"": [5, 4, 3, 2, 1], ""expected"": ""10 14"" }
34+
]
35+
") ?? [];
36+
}
1737

1838
[TestMethod]
1939
public void testMiniMaxSum()
2040
{
2141
string? result;
2242

23-
foreach (MiniMaxSumTestCase test in tests)
43+
foreach (MiniMaxSumTestCase test in testCases)
2444
{
25-
result = MiniMaxSum.miniMaxSum(test.input);
26-
Assert.AreEqual(test.expected, result);
45+
result = MiniMaxSum.miniMaxSum(test.Input);
46+
Assert.AreEqual(test.Expected, result);
2747
}
2848
}
2949

0 commit comments

Comments
 (0)