Skip to content

Commit

Permalink
Merge pull request #634 from sir-gon/feature/new_year_chaos
Browse files Browse the repository at this point in the history
[REFACTOR] Adjusted the interface to match what hackerrank expects.
  • Loading branch information
sir-gon authored Feb 17, 2025
2 parents cb0142f + 14e5bca commit 711ad34
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
29 changes: 21 additions & 8 deletions src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
};
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Original file line number Diff line number Diff line change
@@ -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"
}
]

0 comments on commit 711ad34

Please sign in to comment.