Skip to content

Commit 7d3cba3

Browse files
Gonzalo Diazsir-gon
Gonzalo Diaz
authored andcommitted
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Count Triplets. TEST data moved to JSON.
1 parent 9dd3326 commit 7d3cba3

8 files changed

+44
-70
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"title": "Sample Test Case 2",
4+
"input": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
6+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
7+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
9+
"r": 1,
10+
"expected": 161700
11+
}
12+
]

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,7 @@ import { logger as console } from '../../../logger.js';
33

44
import { countTriplets } from './count_triplets_1_bruteforce.js';
55

6-
const SMALL_TEST_CASES = [
7-
{
8-
title: 'Sample Test Case 0',
9-
input: [1, 2, 2, 4],
10-
r: 2,
11-
expected: 2
12-
},
13-
{
14-
title: 'Sample Test Case 1',
15-
input: [1, 3, 9, 9, 27, 81],
16-
r: 3,
17-
expected: 6
18-
},
19-
{
20-
title: 'Sample Test Case 1 (unsorted)',
21-
input: [9, 3, 1, 81, 9, 27],
22-
r: 3,
23-
expected: 1
24-
},
25-
{
26-
title: 'Sample Test Case 12',
27-
input: [1, 5, 5, 25, 125],
28-
r: 5,
29-
expected: 4
30-
}
31-
];
6+
import SMALL_TEST_CASES from './count_triplets_1.small.testcases.json';
327

338
describe('count_triplets_1', () => {
349
it('countTriplets test cases', () => {

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.js

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

4-
import { countTriplets } from './count_triplets_1_optmized.js';
5-
import SMALL_TEST_CASES from './count_triplets_1_testcases.json';
6-
7-
const BIG_TEST_CASES = [
8-
{
9-
title: 'Sample Test Case 2',
10-
input: [
11-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
14-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
15-
],
16-
r: 1,
17-
expected: 161700
18-
}
19-
];
4+
import CountTriplets from './count_triplets_1_optmized.js';
5+
import SMALL_TEST_CASES from './count_triplets_1.small.testcases.json';
6+
import BIG_TEST_CASES from './count_triplets_1.big.testcases.json';
207

218
describe('count_triplets_1 (optimized)', () => {
229
it('countTriplets small test cases', () => {
2310
expect.assertions(4);
2411

2512
SMALL_TEST_CASES.forEach((test) => {
26-
const answer = countTriplets(test.input, test.r);
13+
const answer = CountTriplets.countTriplets(test.input, test.r);
2714

2815
console.debug(
2916
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
@@ -37,7 +24,7 @@ describe('count_triplets_1 (optimized)', () => {
3724
expect.assertions(1);
3825

3926
BIG_TEST_CASES.forEach((test) => {
40-
const answer = countTriplets(test.input, test.r);
27+
const answer = CountTriplets.countTriplets(test.input, test.r);
4128

4229
console.debug(
4330
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md]]
44
*/
55

6-
export function countTriplets(arr, ratio) {
6+
function countTriplets(arr, ratio) {
77
let triplets = 0;
88

99
const aCounter = arr.reduce((accumulator, entry) => {

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

Lines changed: 2 additions & 2 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,7 +27,7 @@ export function checkMagazineCompute(magazine, note) {
2727
return true;
2828
}
2929

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

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

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
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.checkMagazine(value.magazine, value.note);
3313

3414
console.debug(
3515
`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)