Skip to content

Commit

Permalink
Merge pull request #627 from sir-gon/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
sir-gon authored Mar 5, 2025
2 parents 6003565 + 67dbda8 commit 50bf4ba
Show file tree
Hide file tree
Showing 21 changed files with 106 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger';

import { hourglassSum } from './2d_array';
import twoDarray from './2d_array';
import TEST_CASES from './2d_array.testcases_test.json';

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

TEST_CASES.forEach((test) => {
const answer = hourglassSum(test.input);
const answer = twoDarray.hourglassSum(test.input);

console.debug(
`gethourGlass(${test.input.toString()}) solution found: ${answer}`
Expand Down
4 changes: 2 additions & 2 deletions src/hackerrank/interview_preparation_kit/arrays/2d_array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/2d_array.md]]
*/

export function gethourGlass(
function gethourGlass(
arr: number[][],
positionX: number,
positionY: number
Expand All @@ -22,7 +22,7 @@ export function gethourGlass(
return result;
}

export function hourglassSum(arr: number[][]): number | null {
function hourglassSum(arr: number[][]): number | null {
let matrixSize = 0;

if (arr?.[0]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { logger as console } from '../../../logger';

import TEST_CASES from './cruch_testcases_test.json';

import { arrayManipulation } from './cruch_bruteforce';
import crush from './cruch_bruteforce';

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

TEST_CASES.forEach((test) => {
const answer = arrayManipulation(test.n, test.queries);
const answer = crush.arrayManipulation(test.n, test.queries);

console.debug(
`arrayManipulation(${test.n}, ${test.queries.toString()}) solution found: ${answer}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

export function arrayManipulation(n: number, queries: number[][]): number {
function arrayManipulation(n: number, queries: number[][]): number {
const LENGTH = n + 1;
const SURROGATE_VALUE = 0;
const result: number[] = Array<number>(LENGTH).fill(SURROGATE_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { logger as console } from '../../../logger';

import TEST_CASES from './cruch_testcases_test.json';

import { arrayManipulation } from './cruch_optimized';
import crush from './cruch_optimized';

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

TEST_CASES.forEach((test) => {
const answer = arrayManipulation(test.n, test.queries);
const answer = crush.arrayManipulation(test.n, test.queries);

console.debug(
`arrayManipulation(${test.n}, ${test.queries.toString()}) solution found: ${answer}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]]
*/

export function arrayManipulation(n: number, queries: number[][]): number {
function arrayManipulation(n: number, queries: number[][]): number {
// why adding 2?
// first slot to adjust 1-based index and
// last slot for storing accumSum result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,30 @@
"title": "Sample Test Case 0",
"n": 5,
"queries": [
[
1,
2,
100
],
[
2,
5,
100
],
[
3,
4,
100
]
[1, 2, 100],
[2, 5, 100],
[3, 4, 100]
],
"expected": 200
},
{
"title": "Sample Test Case 1",
"n": 10,
"queries": [
[
1,
5,
3
],
[
4,
8,
7
],
[
6,
9,
1
]
[1, 5, 3],
[4, 8, 7],
[6, 9, 1]
],
"expected": 10
},
{
"title": "Sample Test Case 3",
"n": 10,
"queries": [
[
2,
6,
8
],
[
3,
5,
7
],
[
1,
8,
1
],
[
5,
9,
15
]
[2, 6, 8],
[3, 5, 7],
[1, 8, 1],
[5, 9, 15]
],
"expected": 31
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger';

import { rotLeft, rotLeftOne } from './ctci_array_left_rotation';
import arrayLeftRotation from './ctci_array_left_rotation';

import ROT_LEFT_ONE_TEST_CASES from './ctci_array_left_rotation.testcases.json';
import ROT_LEFT_TEST_CASES from './ctci_array_left_rotation.testcases.json';

describe('ctci_array_left_rotation', () => {
it('rotLeftOne Test Cases', () => {
expect.assertions(5);

ROT_LEFT_ONE_TEST_CASES.forEach((test) => {
const input = test.numbers;
const answer = rotLeftOne(input);

console.debug(
`rotLeftOne(${test.numbers.toString()}) solution found: ${answer.toString()}`
);
interface RotLeftTestCase {
title: string;
input: number[];
d_rotations: number;
expected: number[];
}

expect(answer).toStrictEqual(test.expected);
});
});
const TEST_CASES: RotLeftTestCase[] = ROT_LEFT_TEST_CASES as RotLeftTestCase[];

describe('ctci_array_left_rotation', () => {
it('rotLeft Test cases', () => {
expect.assertions(1);
expect.assertions(8);

const ROT_LEFT_TEST_CASES = [
{ numbers: [1, 2, 3, 4, 5], d_rotations: 4, expected: [5, 1, 2, 3, 4] }
];

ROT_LEFT_TEST_CASES.forEach((value) => {
const answer = rotLeft(value.numbers, value.d_rotations);
TEST_CASES.forEach((test: RotLeftTestCase) => {
const answer = arrayLeftRotation.rotLeft(
test.input,
Number(test.d_rotations)
);

console.debug(
`rotLeft(${value.numbers.toString()}) solution found: ${answer.toString()}`
`rotLeft(${test.input.toString()}) solution found: ${test.expected.toString()}`
);

expect(answer).toStrictEqual(value.expected);
expect(answer).toStrictEqual(test.expected);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[
{"numbers": [1, 2, 3, 4, 5], "expected": [2, 3, 4, 5, 1]},
{"numbers": [2, 3, 4, 5, 1], "expected": [3, 4, 5, 1, 2]},
{"numbers": [3, 4, 5, 1, 2], "expected": [4, 5, 1, 2, 3]},
{"numbers": [4, 5, 1, 2, 3], "expected": [5, 1, 2, 3, 4]},
{"numbers": [5, 1, 2, 3, 4], "expected": [1, 2, 3, 4, 5]}
{"title": "Own 0", "input": [1, 2, 3, 4, 5], "d_rotations": 1, "expected": [2, 3, 4, 5, 1]},
{"title": "Own 1", "input": [2, 3, 4, 5, 1], "d_rotations": 1, "expected": [3, 4, 5, 1, 2]},
{"title": "Own 2", "input": [3, 4, 5, 1, 2], "d_rotations": 1, "expected": [4, 5, 1, 2, 3]},
{"title": "Own 3", "input": [4, 5, 1, 2, 3], "d_rotations": 1, "expected": [5, 1, 2, 3, 4]},
{"title": "Own 4", "input": [5, 1, 2, 3, 4], "d_rotations": 1, "expected": [1, 2, 3, 4, 5]},
{"title": "Sample Test case 0", "input": [1, 2, 3, 4, 5], "d_rotations": 4, "expected": [5, 1, 2, 3, 4]},
{"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]},
{"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]}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]]
*/

export function rotLeftOne(aNumbers: number[]): number[] {
function rotLeftOne(aNumbers: number[]): number[] {
const first = aNumbers.shift();
if (first !== undefined) {
aNumbers.push(first);
Expand All @@ -11,7 +11,7 @@ export function rotLeftOne(aNumbers: number[]): number[] {
return aNumbers;
}

export function rotLeft(aNumbers: number[], dRotations: number): number[] {
function rotLeft(aNumbers: number[], dRotations: number): number[] {
let output = [...aNumbers];

for (let i = 0; i < dRotations; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger';

import { minimumSwaps } from './minimum_swaps_2';
import ms2 from './minimum_swaps_2';

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

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

TEST_CASES.forEach((test) => {
const answer = minimumSwaps(test.input);
const answer = ms2.minimumSwaps(test.input);

console.debug(
`minimumSwaps(${test.input.toString()}) solution found: ${answer}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md]]
*/

export function minimumSwaps(arr: number[]): number {
function minimumSwaps(arr: number[]): number {
const indexedGroup = arr.map((x) => x - 1);
let swaps = 0;
let index = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger';

import { minimumBribesTransform } from './new_year_chaos';
import nyc from './new_year_chaos';

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

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

TEST_CASES.forEach((value) => {
const answer = minimumBribesTransform(value.input);
const answer = nyc.minimumBribesText(value.input);
nyc.minimumBribes(value.input);

console.debug(
`minimumBribesTransform(${value.input.toString()}) solution found: ${answer}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
[
{
"title": "Test Case 0-0",
"input": [2, 1, 5, 3, 4],
"expected": 3
},
{
"title": "Test Case 0-1",
"input": [2, 5, 1, 3, 4],
"expected": "Too chaotic"
},
{
"title": "Test Case 1-1",
"input": [5, 1, 2, 3, 7, 8, 6, 4],
"expected": "Too chaotic"
},
{
"title": "Test Case 1-2",
"input": [1, 2, 5, 3, 7, 8, 6, 4],
"expected": 7
},
{
"title": "Test Case 2",
"input": [1, 2, 5, 3, 4, 7, 8, 6],
"expected": 4
}
]
{
"title": "Test Case 0-0",
"input": [2, 1, 5, 3, 4],
"expected": "3"
},
{
"title": "Test Case 0-1",
"input": [2, 5, 1, 3, 4],
"expected": "Too chaotic"
},
{
"title": "Test Case 1-1",
"input": [5, 1, 2, 3, 7, 8, 6, 4],
"expected": "Too chaotic"
},
{
"title": "Test Case 1-2",
"input": [1, 2, 5, 3, 7, 8, 6, 4],
"expected": "7"
},
{
"title": "Test Case 2",
"input": [1, 2, 5, 3, 4, 7, 8, 6],
"expected": "4"
}
]
Loading

0 comments on commit 50bf4ba

Please sign in to comment.