Skip to content

Commit daadeba

Browse files
author
Gonzalo Diaz
committed
[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 c382e63 commit daadeba

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-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: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,34 @@
11
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
22

3+
using Newtonsoft.Json;
4+
35
[TestClass]
46
public class RansomNoteTest
57
{
68
public class RansomNoteTestCase
79
{
8-
public string title = "";
9-
public List<string> magazine = [];
10-
public List<string> note = [];
11-
public string expected = "";
10+
public string title { get; set; }
11+
public List<string> magazine { get; set; }
12+
public List<string> note { get; set; }
13+
public string expected { get; set; }
1214
}
1315

14-
public class ArraysLeftRotationsTestCase
16+
private List<RansomNoteTestCase> testCases { get; set; }
17+
18+
[TestInitialize]
19+
public void testInitialize()
1520
{
16-
public List<int> input = []; public int d; public List<int> expected = [];
21+
testCases = JsonConvert.DeserializeObject<List<RansomNoteTestCase>>(
22+
JsonLoader.resourceLoad("hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci_ransom_note.testcases.json")
23+
) ?? [];
1724
}
1825

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-
4326
[TestMethod]
4427
public void testCheckMagazine()
4528
{
4629
string result;
4730

48-
foreach (RansomNoteTestCase test in tests)
31+
foreach (RansomNoteTestCase test in testCases)
4932
{
5033
result = RansomNote.checkMagazine(test.magazine, test.note);
5134
Assert.AreEqual(test.expected, result);

0 commit comments

Comments
 (0)