Skip to content

Commit eafe115

Browse files
Gonzalo Diazsir-gon
authored andcommitted
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Hash Tables: Ransom Note.
* TEST data moved to JSON. * Adjusted the interface to match what hackerrank expects.
1 parent b68cb48 commit eafe115

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
const __YES__ = 'Yes';
66
const __NO__ = 'No';
77

8-
export function checkMagazineCompute(magazine, note) {
8+
function checkMagazineCompute(magazine, note) {
99
const dictionary = {};
1010

1111
for (const word of magazine) {
@@ -27,8 +27,12 @@ export function checkMagazineCompute(magazine, note) {
2727
return true;
2828
}
2929

30-
export function checkMagazine(magazine, note) {
30+
function checkMagazineText(magazine, note) {
3131
return checkMagazineCompute(magazine, note) ? __YES__ : __NO__;
3232
}
3333

34-
export default { checkMagazine };
34+
function checkMagazine(magazine, note) {
35+
console.log(checkMagazineCompute(magazine, note) ? __YES__ : __NO__);
36+
}
37+
38+
export default { checkMagazine, checkMagazineText };

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.test.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,16 @@
11
import { describe, expect, it } from '@jest/globals';
22
import { logger as console } from '../../../logger.js';
33

4-
import { checkMagazine } from './ctci-ransom-note.js';
5-
6-
const TEST_CASES = [
7-
{
8-
title: 'Sample Test Case 0',
9-
magazine: ['give', 'me', 'one', 'grand', 'today', 'night'],
10-
note: ['give', 'one', 'grand', 'today'],
11-
expected: 'Yes'
12-
},
13-
{
14-
title: 'Sample Test Case 1',
15-
magazine: ['two', 'times', 'three', 'is', 'not', 'four'],
16-
note: ['two', 'times', 'two', 'is', 'four'],
17-
expected: 'No'
18-
},
19-
{
20-
title: 'Sample Test',
21-
magazine: ['two', 'two', 'times', 'three', 'is', 'not', 'four'],
22-
note: ['two', 'times', 'two', 'is', 'four'],
23-
expected: 'Yes'
24-
}
25-
];
4+
import ransomNote from './ctci-ransom-note.js';
5+
import TEST_CASES from './ctci-ransom-note.testcases.json';
266

277
describe('ctci_ransom_note', () => {
288
it('checkMagazine test cases', () => {
299
expect.assertions(3);
3010

3111
TEST_CASES.forEach((value) => {
32-
const answer = checkMagazine(value.magazine, value.note);
12+
const answer = ransomNote.checkMagazineText(value.magazine, value.note);
13+
ransomNote.checkMagazine(value.magazine, value.note);
3314

3415
console.debug(
3516
`checkMagazine(${value.magazine}, ${value.note}) solution found: ${answer}`
Lines changed: 20 additions & 0 deletions
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+
]

0 commit comments

Comments
 (0)