Skip to content

Commit fcd0478

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Hash Tables: Ransom Note. Load test case data from JSON.
JSON load as Resource: https://khalidabuhakmeh.com/how-to-use-embedded-resources-in-dotnet
1 parent 65a47c9 commit fcd0478

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"magazine": ["give", "me", "one", "grand", "today", "night"],
5+
"note": ["give", "one", "grand", "today"],
6+
"expected": "Yes"
7+
},
8+
{
9+
"title": "Sample Test Case 1",
10+
"magazine": ["two", "times", "three", "is", "not", "four"],
11+
"note": ["two", "times", "two", "is", "four"],
12+
"expected": "No"
13+
},
14+
{
15+
"title": "Sample Test",
16+
"magazine": ["two", "two", "times", "three", "is", "not", "four"],
17+
"note": ["two", "times", "two", "is", "four"],
18+
"expected": "Yes"
19+
}
20+
]

algorithm_exercises_csharp_test/algorithm_exercises_csharp_test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@
5757

5858
<ItemGroup>
5959
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json" />
60+
<EmbeddedResource Include="Resources/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci_ransom_note.testcases.json" />
6061
</ItemGroup>
6162
</Project>

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

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,28 @@ public class RansomNoteTest
55
{
66
public class RansomNoteTestCase
77
{
8-
public string title = "";
9-
public List<string> magazine = [];
10-
public List<string> note = [];
11-
public string expected = "";
8+
public string title { get; set; } = default!;
9+
public List<string> magazine { get; set; } = default!;
10+
public List<string> note { get; set; } = default!;
11+
public string expected { get; set; } = default!;
1212
}
1313

14-
public class ArraysLeftRotationsTestCase
14+
private List<RansomNoteTestCase> testCases { get; set; } = default!;
15+
16+
[TestInitialize]
17+
public void testInitialize()
1518
{
16-
public List<int> input = []; public int d; public List<int> expected = [];
19+
testCases = JsonLoader.resourceLoad<List<RansomNoteTestCase>>(
20+
"hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci_ransom_note.testcases.json"
21+
);
1722
}
1823

19-
private static readonly RansomNoteTestCase[] tests = [
20-
new()
21-
{
22-
title = "Sample Test Case 0",
23-
magazine = ["give", "me", "one", "grand", "today", "night"],
24-
note = ["give", "one", "grand", "today"],
25-
expected = "Yes"
26-
},
27-
new()
28-
{
29-
title = "Sample Test Case 1",
30-
magazine = ["two", "times", "three", "is", "not", "four"],
31-
note = ["two", "times", "two", "is", "four"],
32-
expected = "No"
33-
},
34-
new()
35-
{
36-
title = "Sample Test",
37-
magazine = ["two", "two", "times", "three", "is", "not", "four"],
38-
note = ["two", "times", "two", "is", "four"],
39-
expected = "Yes"
40-
},
41-
];
42-
4324
[TestMethod]
4425
public void testCheckMagazine()
4526
{
4627
string result;
4728

48-
foreach (RansomNoteTestCase test in tests)
29+
foreach (RansomNoteTestCase test in testCases)
4930
{
5031
result = RansomNote.checkMagazine(test.magazine, test.note);
5132
Assert.AreEqual(test.expected, result);

0 commit comments

Comments
 (0)