Skip to content

Commit e01cf89

Browse files
authored
Merge pull request #652 from sir-gon/refactor
Refactor
2 parents 9421121 + 7f162f3 commit e01cf89

11 files changed

+47
-84
lines changed

src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]]
33
*/
44

5-
export function rotLeftOne(aNumbers) {
5+
function rotLeftOne(aNumbers) {
66
const first = aNumbers.shift();
77
if (first !== undefined) {
88
aNumbers.push(first);
@@ -11,7 +11,7 @@ export function rotLeftOne(aNumbers) {
1111
return aNumbers;
1212
}
1313

14-
export function rotLeft(aNumbers, dRotations) {
14+
function rotLeft(aNumbers, dRotations) {
1515
let output = [...aNumbers];
1616

1717
for (let i = 0; i < dRotations; i++) {
Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
11
import { describe, expect, it } from '@jest/globals';
22
import { logger as console } from '../../../logger.js';
33

4-
import { rotLeft, rotLeftOne } from './ctci_array_left_rotation.js';
4+
import arrayLeftRotation from './ctci_array_left_rotation.js';
55

6-
const ROT_LEFT_ONE_TEST_CASES = [
7-
{ numbers: [1, 2, 3, 4, 5], expected: [2, 3, 4, 5, 1] },
8-
{ numbers: [2, 3, 4, 5, 1], expected: [3, 4, 5, 1, 2] },
9-
{ numbers: [3, 4, 5, 1, 2], expected: [4, 5, 1, 2, 3] },
10-
{ numbers: [4, 5, 1, 2, 3], expected: [5, 1, 2, 3, 4] },
11-
{ numbers: [5, 1, 2, 3, 4], expected: [1, 2, 3, 4, 5] }
12-
];
13-
14-
const ROT_LEFT_TEST_CASES = [
15-
{ numbers: [1, 2, 3, 4, 5], d_rotations: 4, expected: [5, 1, 2, 3, 4] }
16-
];
6+
import ROT_LEFT_TEST_CASES from './ctci_array_left_rotation.testcases.json';
177

188
describe('ctci_array_left_rotation', () => {
19-
it('rotLeftOne Test Cases', () => {
20-
expect.assertions(5);
21-
22-
ROT_LEFT_ONE_TEST_CASES.forEach((value) => {
23-
const answer = rotLeftOne(value.numbers);
24-
25-
console.debug(`rotLeftOne(${value.numbers}) solution found: ${answer}`);
26-
27-
expect(answer).toStrictEqual(value.expected);
28-
});
29-
});
30-
319
it('rotLeft Test cases', () => {
32-
expect.assertions(1);
10+
expect.assertions(8);
3311

34-
ROT_LEFT_TEST_CASES.forEach((value) => {
35-
const answer = rotLeft(value.numbers, value.d_rotations);
12+
ROT_LEFT_TEST_CASES.forEach((test) => {
13+
const answer = arrayLeftRotation.rotLeft(test.input, test.d_rotations);
3614

37-
console.debug(`rotLeft(${value.numbers}) solution found: ${answer}`);
15+
console.debug(`rotLeft(${test.numbers}) solution found: ${answer}`);
3816

39-
expect(answer).toStrictEqual(value.expected);
17+
expect(answer).toStrictEqual(test.expected);
4018
});
4119
});
4220
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{"title": "Own 0", "input": [1, 2, 3, 4, 5], "d_rotations": 1, "expected": [2, 3, 4, 5, 1]},
3+
{"title": "Own 1", "input": [2, 3, 4, 5, 1], "d_rotations": 1, "expected": [3, 4, 5, 1, 2]},
4+
{"title": "Own 2", "input": [3, 4, 5, 1, 2], "d_rotations": 1, "expected": [4, 5, 1, 2, 3]},
5+
{"title": "Own 3", "input": [4, 5, 1, 2, 3], "d_rotations": 1, "expected": [5, 1, 2, 3, 4]},
6+
{"title": "Own 4", "input": [5, 1, 2, 3, 4], "d_rotations": 1, "expected": [1, 2, 3, 4, 5]},
7+
{"title": "Sample Test case 0", "input": [1, 2, 3, 4, 5], "d_rotations": 4, "expected": [5, 1, 2, 3, 4]},
8+
{"title": "Sample Test case 1", "input": [41, 73, 89, 7, 10, 1, 59, 58, 84, 77, 77, 97, 58, 1, 86, 58, 26, 10, 86, 51], "d_rotations": 10, "expected": [77, 97, 58, 1, 86, 58, 26, 10, 86, 51, 41, 73, 89, 7, 10, 1, 59, 58, 84, 77]},
9+
{"title": "Sample Test case 2", "input": [33, 47, 70, 37, 8, 53, 13, 93, 71, 72, 51, 100, 60, 87, 97], "d_rotations": 13, "expected": [87, 97, 33, 47, 70, 37, 8, 53, 13, 93, 71, 72, 51, 100, 60]}
10+
]

src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md]]
33
*/
44

5-
export function minimumSwaps(arr) {
5+
function minimumSwaps(arr) {
66
const indexedGroup = arr.map((x) => x - 1);
77
let swaps = 0;
88
let index = 0;

src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.test.js

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

4-
import { minimumSwaps } from './minimum_swaps_2.js';
4+
import ms2 from './minimum_swaps_2.js';
55

6-
const TEST_CASES = [
7-
{ title: 'Sample input 0', input: [4, 3, 1, 2], expected: 3 },
8-
{ title: 'Sample input 1', input: [2, 3, 4, 1, 5], expected: 3 },
9-
{ title: 'Sample input 2', input: [1, 3, 5, 2, 4, 6, 7], expected: 3 }
10-
];
6+
import TEST_CASES from './minimum_swaps_2.testcases.json';
117

128
describe('minimum swaps 2', () => {
139
it('minimumSwaps', () => {
1410
expect.assertions(3);
1511

1612
TEST_CASES.forEach((test) => {
17-
const answer = minimumSwaps(test.input);
13+
const answer = ms2.minimumSwaps(test.input);
1814

1915
console.debug(`minimumSwaps(${test.input}) solution found: ${answer}`);
2016

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{"title": "Sample input 0", "input": [4, 3, 1, 2], "expected": 3},
3+
{"title": "Sample input 1", "input": [2, 3, 4, 1, 5], "expected": 3},
4+
{"title": "Sample input 2", "input": [1, 3, 5, 2, 4, 6, 7], "expected": 3}
5+
]
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) => {

0 commit comments

Comments
 (0)