Skip to content

Refactor #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
function rotLeftOne(aNumbers) {
const first = aNumbers.shift();
if (first !== undefined) {
aNumbers.push(first);
Expand All @@ -11,7 +11,7 @@ export function rotLeftOne(aNumbers) {
return aNumbers;
}

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

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

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

const ROT_LEFT_ONE_TEST_CASES = [
{ 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] }
];

const ROT_LEFT_TEST_CASES = [
{ numbers: [1, 2, 3, 4, 5], d_rotations: 4, expected: [5, 1, 2, 3, 4] }
];
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((value) => {
const answer = rotLeftOne(value.numbers);

console.debug(`rotLeftOne(${value.numbers}) solution found: ${answer}`);

expect(answer).toStrictEqual(value.expected);
});
});

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

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

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

expect(answer).toStrictEqual(value.expected);
expect(answer).toStrictEqual(test.expected);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{"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 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]}
]
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) {
function minimumSwaps(arr) {
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,20 +1,16 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger.js';

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

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{"title": "Sample input 0", "input": [4, 3, 1, 2], "expected": 3},
{"title": "Sample input 1", "input": [2, 3, 4, 1, 5], "expected": 3},
{"title": "Sample input 2", "input": [1, 3, 5, 2, 4, 6, 7], "expected": 3}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"title": "Sample Test Case 2",
"input": [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, 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, 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, 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],
"r": 1,
"expected": 161700
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,7 @@ import { logger as console } from '../../../logger.js';

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

const SMALL_TEST_CASES = [
{
title: 'Sample Test Case 0',
input: [1, 2, 2, 4],
r: 2,
expected: 2
},
{
title: 'Sample Test Case 1',
input: [1, 3, 9, 9, 27, 81],
r: 3,
expected: 6
},
{
title: 'Sample Test Case 1 (unsorted)',
input: [9, 3, 1, 81, 9, 27],
r: 3,
expected: 1
},
{
title: 'Sample Test Case 12',
input: [1, 5, 5, 25, 125],
r: 5,
expected: 4
}
];
import SMALL_TEST_CASES from './count_triplets_1.small.testcases.json';

describe('count_triplets_1', () => {
it('countTriplets test cases', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger.js';

import { countTriplets } from './count_triplets_1_optmized.js';
import SMALL_TEST_CASES from './count_triplets_1_testcases.json';

const BIG_TEST_CASES = [
{
title: 'Sample Test Case 2',
input: [
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,
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,
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,
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
],
r: 1,
expected: 161700
}
];
import CountTriplets from './count_triplets_1_optmized.js';
import SMALL_TEST_CASES from './count_triplets_1.small.testcases.json';
import BIG_TEST_CASES from './count_triplets_1.big.testcases.json';

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

SMALL_TEST_CASES.forEach((test) => {
const answer = countTriplets(test.input, test.r);
const answer = CountTriplets.countTriplets(test.input, test.r);

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

BIG_TEST_CASES.forEach((test) => {
const answer = countTriplets(test.input, test.r);
const answer = CountTriplets.countTriplets(test.input, test.r);

console.debug(
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md]]
*/

export function countTriplets(arr, ratio) {
function countTriplets(arr, ratio) {
let triplets = 0;

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