Skip to content

Commit 4f26bf6

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Greedy Algorithms: Luck Balance. Load test case data from JSON.
JSON load as Resource: https://khalidabuhakmeh.com/how-to-use-embedded-resources-in-dotnet
1 parent ad7622d commit 4f26bf6

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"k": 3,
5+
"contests": [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]],
6+
"expected": 29
7+
}
8+
]

algorithm_exercises_csharp_test/algorithm_exercises_csharp_test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@
6262
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json" />
6363
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci_ransom_note.testcases.json" />
6464
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.testcases.json" />
65+
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/greedy_algorithms/luck_balance.testcases.json" />
6566
</ItemGroup>
6667
</Project>

algorithm_exercises_csharp_test/src/hackerrank/interview_preparation_kit/greedy_algorithms/LuckBalance.Test.cs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,22 @@ public class LuckBalanceTestCase
1111
public int expected;
1212
}
1313

14-
private static readonly LuckBalanceTestCase[] tests = [
15-
new()
16-
{
17-
title = "Sample Test case 0",
18-
k = 3,
19-
contests = [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]],
20-
expected = 29
21-
},
22-
new()
23-
{
24-
title = "Sample Test case 1",
25-
k = 5,
26-
contests = [[13, 1], [10, 1], [9, 1], [8, 1], [13, 1], [12, 1], [18, 1], [13, 1]],
27-
expected = 42
28-
},
29-
new()
30-
{
31-
title = "Sample Test case 2",
32-
k = 2,
33-
contests = [[5, 1], [4, 0], [6, 1], [2, 1], [8, 0]],
34-
expected = 21
35-
}
36-
];
14+
private List<LuckBalanceTestCase> testCases { get; set; } = default!;
15+
16+
[TestInitialize]
17+
public void testInitialize()
18+
{
19+
testCases = JsonLoader.resourceLoad<List<LuckBalanceTestCase>>(
20+
"hackerrank/interview_preparation_kit/greedy_algorithms/luck_balance.testcases.json"
21+
) ?? [];
22+
}
3723

3824
[TestMethod]
3925
public void testLuckBalance()
4026
{
4127
int result;
4228

43-
foreach (LuckBalanceTestCase test in tests)
29+
foreach (LuckBalanceTestCase test in testCases)
4430
{
4531
result = LuckBalance.luckBalance(test.k, test.contests);
4632
Assert.AreEqual(

0 commit comments

Comments
 (0)