diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.js b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.js index 1890424c..e547c26f 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.js +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.js @@ -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); @@ -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++) { diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.js b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.js index 8a66a45a..e673f110 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.js +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.js @@ -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); }); }); }); diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json new file mode 100644 index 00000000..d78f6838 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json @@ -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]} +] diff --git a/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.js b/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.js index 71d1a341..7cf6068b 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.js +++ b/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.js @@ -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; diff --git a/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.test.js b/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.test.js index 9c784a32..bfbe1e72 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.test.js +++ b/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.test.js @@ -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}`); diff --git a/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.testcases.json b/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.testcases.json new file mode 100644 index 00000000..6a814023 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.testcases.json @@ -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} +] diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.big.testcases.json b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.big.testcases.json new file mode 100644 index 00000000..f38639cd --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.big.testcases.json @@ -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 + } +] diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_testcases.json b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.small.testcases.json similarity index 100% rename from src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_testcases.json rename to src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1.small.testcases.json diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.js b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.js index 590330ef..264aea24 100644 --- a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.js +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.js @@ -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', () => { diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.js b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.js index 78890b9e..0c1ea88e 100644 --- a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.js +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.js @@ -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}` @@ -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}` diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.js b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.js index bef443a1..8045ba91 100644 --- a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.js +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.js @@ -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) => {