diff --git a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.js b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.js index f10f4f53..06d53d1d 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.js +++ b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.js @@ -2,19 +2,23 @@ * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md]] */ -export const TOO_CHAOTIC_ERROR = 'Too chaotic'; +const TOO_CHAOTIC_ERROR = 'Too chaotic'; +const NEW_YEAR_CHAOS_TOLERANCE = 2; -export function minimumBribes(q) { +function minimumBribesCalculate(q) { let bribes = 0; let i = 0; q.forEach((value) => { const position = i + 1; - if (value - position > 2) { + if (value - position > NEW_YEAR_CHAOS_TOLERANCE) { throw new Error(TOO_CHAOTIC_ERROR); } - const fragment = q.slice(Math.max(value - 2, 0), i); + const fragment = q.slice( + Math.min(Math.max(value - NEW_YEAR_CHAOS_TOLERANCE, 0), i), + i + ); fragment.forEach((k) => { if (k > value) { @@ -27,12 +31,21 @@ export function minimumBribes(q) { return bribes; } -export function minimumBribesTransform(queue) { +function minimumBribesText(queue) { try { - return minimumBribes(queue).toString(10); + return minimumBribesCalculate(queue).toString(10); } catch (e) { - return TOO_CHAOTIC_ERROR; + return e.message; } } -export default { minimumBribes, minimumBribesTransform, TOO_CHAOTIC_ERROR }; +function minimumBribes(q) { + console.log(minimumBribesText(q)); +} + +export default { + minimumBribes, + minimumBribesCalculate, + minimumBribesText, + TOO_CHAOTIC_ERROR +}; diff --git a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.test.js b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.test.js index 0d6201b3..81335ff7 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.test.js +++ b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.test.js @@ -1,36 +1,23 @@ import { describe, expect, it } from '@jest/globals'; import { logger as console } from '../../../logger.js'; -import { minimumBribesTransform, TOO_CHAOTIC_ERROR } from './new_year_chaos.js'; +import nyc from './new_year_chaos.js'; -const TEST_CASES = [ - { 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_ERROR - }, - { - title: 'Test Case 1-1', - input: [5, 1, 2, 3, 7, 8, 6, 4], - expected: TOO_CHAOTIC_ERROR - }, - { 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' } -]; +import TEST_CASES from './new_year_chaos.testcases.json'; describe('new_year_chaos', () => { it('minimumBribes Test Cases', () => { expect.assertions(5); - TEST_CASES.forEach((value) => { - const answer = minimumBribesTransform(value.input); + TEST_CASES.forEach((test) => { + const answer = nyc.minimumBribesText(test.input); + nyc.minimumBribes(test.input); console.debug( - `minimumBribesTransform(${value.input}) solution found: ${answer}` + `minimumBribesTransform(${test.input}) solution found: ${answer}` ); - expect(answer).toStrictEqual(value.expected); + expect(answer).toStrictEqual(test.expected); }); }); }); diff --git a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json new file mode 100644 index 00000000..90d3bcc4 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json @@ -0,0 +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" + } + ]