Skip to content

Commit 50bf4ba

Browse files
authored
Merge pull request #627 from sir-gon/refactor
Refactor
2 parents 6003565 + 67dbda8 commit 50bf4ba

21 files changed

+106
-142
lines changed

src/hackerrank/interview_preparation_kit/arrays/2d_array.test.ts

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

4-
import { hourglassSum } from './2d_array';
4+
import twoDarray from './2d_array';
55
import TEST_CASES from './2d_array.testcases_test.json';
66

77
describe('arrays: 2d Array hourglassSum', () => {
88
it('hourglassSum Test Cases', () => {
99
expect.assertions(3);
1010

1111
TEST_CASES.forEach((test) => {
12-
const answer = hourglassSum(test.input);
12+
const answer = twoDarray.hourglassSum(test.input);
1313

1414
console.debug(
1515
`gethourGlass(${test.input.toString()}) solution found: ${answer}`

src/hackerrank/interview_preparation_kit/arrays/2d_array.ts

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/2d_array.md]]
33
*/
44

5-
export function gethourGlass(
5+
function gethourGlass(
66
arr: number[][],
77
positionX: number,
88
positionY: number
@@ -22,7 +22,7 @@ export function gethourGlass(
2222
return result;
2323
}
2424

25-
export function hourglassSum(arr: number[][]): number | null {
25+
function hourglassSum(arr: number[][]): number | null {
2626
let matrixSize = 0;
2727

2828
if (arr?.[0]) {

src/hackerrank/interview_preparation_kit/arrays/cruch_bruteforce.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { logger as console } from '../../../logger';
33

44
import TEST_CASES from './cruch_testcases_test.json';
55

6-
import { arrayManipulation } from './cruch_bruteforce';
6+
import crush from './cruch_bruteforce';
77

88
describe('arrays: crush (bruteforce) small cases', () => {
99
it('arrayManipulation Test Cases', () => {
1010
expect.assertions(3);
1111

1212
TEST_CASES.forEach((test) => {
13-
const answer = arrayManipulation(test.n, test.queries);
13+
const answer = crush.arrayManipulation(test.n, test.queries);
1414

1515
console.debug(
1616
`arrayManipulation(${test.n}, ${test.queries.toString()}) solution found: ${answer}`

src/hackerrank/interview_preparation_kit/arrays/cruch_bruteforce.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { logger as console } from '../../../logger';
77

8-
export function arrayManipulation(n: number, queries: number[][]): number {
8+
function arrayManipulation(n: number, queries: number[][]): number {
99
const LENGTH = n + 1;
1010
const SURROGATE_VALUE = 0;
1111
const result: number[] = Array<number>(LENGTH).fill(SURROGATE_VALUE);

src/hackerrank/interview_preparation_kit/arrays/cruch_optimized.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { logger as console } from '../../../logger';
33

44
import TEST_CASES from './cruch_testcases_test.json';
55

6-
import { arrayManipulation } from './cruch_optimized';
6+
import crush from './cruch_optimized';
77

88
describe('arrays: crush (optimized)', () => {
99
it('arrayManipulation Test Cases', () => {
1010
expect.assertions(3);
1111

1212
TEST_CASES.forEach((test) => {
13-
const answer = arrayManipulation(test.n, test.queries);
13+
const answer = crush.arrayManipulation(test.n, test.queries);
1414

1515
console.debug(
1616
`arrayManipulation(${test.n}, ${test.queries.toString()}) solution found: ${answer}`

src/hackerrank/interview_preparation_kit/arrays/cruch_optimized.ts

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/crush.md]]
33
*/
44

5-
export function arrayManipulation(n: number, queries: number[][]): number {
5+
function arrayManipulation(n: number, queries: number[][]): number {
66
// why adding 2?
77
// first slot to adjust 1-based index and
88
// last slot for storing accumSum result

src/hackerrank/interview_preparation_kit/arrays/cruch_testcases_test.json

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,30 @@
33
"title": "Sample Test Case 0",
44
"n": 5,
55
"queries": [
6-
[
7-
1,
8-
2,
9-
100
10-
],
11-
[
12-
2,
13-
5,
14-
100
15-
],
16-
[
17-
3,
18-
4,
19-
100
20-
]
6+
[1, 2, 100],
7+
[2, 5, 100],
8+
[3, 4, 100]
219
],
2210
"expected": 200
2311
},
2412
{
2513
"title": "Sample Test Case 1",
2614
"n": 10,
2715
"queries": [
28-
[
29-
1,
30-
5,
31-
3
32-
],
33-
[
34-
4,
35-
8,
36-
7
37-
],
38-
[
39-
6,
40-
9,
41-
1
42-
]
16+
[1, 5, 3],
17+
[4, 8, 7],
18+
[6, 9, 1]
4319
],
4420
"expected": 10
4521
},
4622
{
4723
"title": "Sample Test Case 3",
4824
"n": 10,
4925
"queries": [
50-
[
51-
2,
52-
6,
53-
8
54-
],
55-
[
56-
3,
57-
5,
58-
7
59-
],
60-
[
61-
1,
62-
8,
63-
1
64-
],
65-
[
66-
5,
67-
9,
68-
15
69-
]
26+
[2, 6, 8],
27+
[3, 5, 7],
28+
[1, 8, 1],
29+
[5, 9, 15]
7030
],
7131
"expected": 31
7232
}
Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
11
import { describe, expect, it } from '@jest/globals';
22
import { logger as console } from '../../../logger';
33

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

6-
import ROT_LEFT_ONE_TEST_CASES from './ctci_array_left_rotation.testcases.json';
6+
import ROT_LEFT_TEST_CASES from './ctci_array_left_rotation.testcases.json';
77

8-
describe('ctci_array_left_rotation', () => {
9-
it('rotLeftOne Test Cases', () => {
10-
expect.assertions(5);
11-
12-
ROT_LEFT_ONE_TEST_CASES.forEach((test) => {
13-
const input = test.numbers;
14-
const answer = rotLeftOne(input);
15-
16-
console.debug(
17-
`rotLeftOne(${test.numbers.toString()}) solution found: ${answer.toString()}`
18-
);
8+
interface RotLeftTestCase {
9+
title: string;
10+
input: number[];
11+
d_rotations: number;
12+
expected: number[];
13+
}
1914

20-
expect(answer).toStrictEqual(test.expected);
21-
});
22-
});
15+
const TEST_CASES: RotLeftTestCase[] = ROT_LEFT_TEST_CASES as RotLeftTestCase[];
2316

17+
describe('ctci_array_left_rotation', () => {
2418
it('rotLeft Test cases', () => {
25-
expect.assertions(1);
19+
expect.assertions(8);
2620

27-
const ROT_LEFT_TEST_CASES = [
28-
{ numbers: [1, 2, 3, 4, 5], d_rotations: 4, expected: [5, 1, 2, 3, 4] }
29-
];
30-
31-
ROT_LEFT_TEST_CASES.forEach((value) => {
32-
const answer = rotLeft(value.numbers, value.d_rotations);
21+
TEST_CASES.forEach((test: RotLeftTestCase) => {
22+
const answer = arrayLeftRotation.rotLeft(
23+
test.input,
24+
Number(test.d_rotations)
25+
);
3326

3427
console.debug(
35-
`rotLeft(${value.numbers.toString()}) solution found: ${answer.toString()}`
28+
`rotLeft(${test.input.toString()}) solution found: ${test.expected.toString()}`
3629
);
3730

38-
expect(answer).toStrictEqual(value.expected);
31+
expect(answer).toStrictEqual(test.expected);
3932
});
4033
});
4134
});
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[
2-
{"numbers": [1, 2, 3, 4, 5], "expected": [2, 3, 4, 5, 1]},
3-
{"numbers": [2, 3, 4, 5, 1], "expected": [3, 4, 5, 1, 2]},
4-
{"numbers": [3, 4, 5, 1, 2], "expected": [4, 5, 1, 2, 3]},
5-
{"numbers": [4, 5, 1, 2, 3], "expected": [5, 1, 2, 3, 4]},
6-
{"numbers": [5, 1, 2, 3, 4], "expected": [1, 2, 3, 4, 5]}
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 1", "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]}
710
]

src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.ts

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: number[]): number[] {
5+
function rotLeftOne(aNumbers: number[]): number[] {
66
const first = aNumbers.shift();
77
if (first !== undefined) {
88
aNumbers.push(first);
@@ -11,7 +11,7 @@ export function rotLeftOne(aNumbers: number[]): number[] {
1111
return aNumbers;
1212
}
1313

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

1717
for (let i = 0; i < dRotations; i++) {

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

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

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

66
import TEST_CASES from './minimum_swaps_2.testcases.json';
77

@@ -10,7 +10,7 @@ describe('minimum swaps 2', () => {
1010
expect.assertions(3);
1111

1212
TEST_CASES.forEach((test) => {
13-
const answer = minimumSwaps(test.input);
13+
const answer = ms2.minimumSwaps(test.input);
1414

1515
console.debug(
1616
`minimumSwaps(${test.input.toString()}) solution found: ${answer}`

src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.ts

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: number[]): number {
5+
function minimumSwaps(arr: number[]): number {
66
const indexedGroup = arr.map((x) => x - 1);
77
let swaps = 0;
88
let index = 0;

src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.test.ts

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

4-
import { minimumBribesTransform } from './new_year_chaos';
4+
import nyc from './new_year_chaos';
55

66
import TEST_CASES from './new_year_chaos.testcases.json';
77

@@ -10,7 +10,8 @@ describe('new_year_chaos', () => {
1010
expect.assertions(5);
1111

1212
TEST_CASES.forEach((value) => {
13-
const answer = minimumBribesTransform(value.input);
13+
const answer = nyc.minimumBribesText(value.input);
14+
nyc.minimumBribes(value.input);
1415

1516
console.debug(
1617
`minimumBribesTransform(${value.input.toString()}) solution found: ${answer}`
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
[
2-
{
3-
"title": "Test Case 0-0",
4-
"input": [2, 1, 5, 3, 4],
5-
"expected": 3
6-
},
7-
{
8-
"title": "Test Case 0-1",
9-
"input": [2, 5, 1, 3, 4],
10-
"expected": "Too chaotic"
11-
},
12-
{
13-
"title": "Test Case 1-1",
14-
"input": [5, 1, 2, 3, 7, 8, 6, 4],
15-
"expected": "Too chaotic"
16-
},
17-
{
18-
"title": "Test Case 1-2",
19-
"input": [1, 2, 5, 3, 7, 8, 6, 4],
20-
"expected": 7
21-
},
22-
{
23-
"title": "Test Case 2",
24-
"input": [1, 2, 5, 3, 4, 7, 8, 6],
25-
"expected": 4
26-
}
27-
]
2+
{
3+
"title": "Test Case 0-0",
4+
"input": [2, 1, 5, 3, 4],
5+
"expected": "3"
6+
},
7+
{
8+
"title": "Test Case 0-1",
9+
"input": [2, 5, 1, 3, 4],
10+
"expected": "Too chaotic"
11+
},
12+
{
13+
"title": "Test Case 1-1",
14+
"input": [5, 1, 2, 3, 7, 8, 6, 4],
15+
"expected": "Too chaotic"
16+
},
17+
{
18+
"title": "Test Case 1-2",
19+
"input": [1, 2, 5, 3, 7, 8, 6, 4],
20+
"expected": "7"
21+
},
22+
{
23+
"title": "Test Case 2",
24+
"input": [1, 2, 5, 3, 4, 7, 8, 6],
25+
"expected": "4"
26+
}
27+
]

0 commit comments

Comments
 (0)