Skip to content

Commit ae1e1bd

Browse files
authored
Merge pull request #635 from sir-gon/feature/new_year_chaos
[REFACTOR] Adjusted the interface to match what hackerrank expects.
2 parents cb0142f + d0937d4 commit ae1e1bd

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md]]
33
*/
44

5-
export const TOO_CHAOTIC_ERROR = 'Too chaotic';
5+
const TOO_CHAOTIC_ERROR = 'Too chaotic';
6+
const NEW_YEAR_CHAOS_TOLERANCE = 2;
67

7-
export function minimumBribes(q) {
8+
function minimumBribesCalculate(q) {
89
let bribes = 0;
910
let i = 0;
1011

1112
q.forEach((value) => {
1213
const position = i + 1;
13-
if (value - position > 2) {
14+
if (value - position > NEW_YEAR_CHAOS_TOLERANCE) {
1415
throw new Error(TOO_CHAOTIC_ERROR);
1516
}
1617

17-
const fragment = q.slice(Math.max(value - 2, 0), i);
18+
const fragment = q.slice(
19+
Math.min(Math.max(value - NEW_YEAR_CHAOS_TOLERANCE, 0), i),
20+
i
21+
);
1822

1923
fragment.forEach((k) => {
2024
if (k > value) {
@@ -27,12 +31,21 @@ export function minimumBribes(q) {
2731
return bribes;
2832
}
2933

30-
export function minimumBribesTransform(queue) {
34+
function minimumBribesText(queue) {
3135
try {
32-
return minimumBribes(queue).toString(10);
36+
return minimumBribesCalculate(queue).toString(10);
3337
} catch (e) {
34-
return TOO_CHAOTIC_ERROR;
38+
return e.message;
3539
}
3640
}
3741

38-
export default { minimumBribes, minimumBribesTransform, TOO_CHAOTIC_ERROR };
42+
function minimumBribes(q) {
43+
console.log(minimumBribesText(q));
44+
}
45+
46+
export default {
47+
minimumBribes,
48+
minimumBribesCalculate,
49+
minimumBribesText,
50+
TOO_CHAOTIC_ERROR
51+
};
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
11
import { describe, expect, it } from '@jest/globals';
22
import { logger as console } from '../../../logger.js';
33

4-
import { minimumBribesTransform, TOO_CHAOTIC_ERROR } from './new_year_chaos.js';
4+
import testingModule from './new_year_chaos.js';
55

6-
const TEST_CASES = [
7-
{ title: 'Test Case 0-0', input: [2, 1, 5, 3, 4], expected: '3' },
8-
{
9-
title: 'Test Case 0-1',
10-
input: [2, 5, 1, 3, 4],
11-
expected: TOO_CHAOTIC_ERROR
12-
},
13-
{
14-
title: 'Test Case 1-1',
15-
input: [5, 1, 2, 3, 7, 8, 6, 4],
16-
expected: TOO_CHAOTIC_ERROR
17-
},
18-
{ title: 'Test Case 1-2', input: [1, 2, 5, 3, 7, 8, 6, 4], expected: '7' },
19-
{ title: 'Test Case 2', input: [1, 2, 5, 3, 4, 7, 8, 6], expected: '4' }
20-
];
6+
import TEST_CASES from './new_year_chaos.testcases.json';
217

228
describe('new_year_chaos', () => {
239
it('minimumBribes Test Cases', () => {
2410
expect.assertions(5);
2511

26-
TEST_CASES.forEach((value) => {
27-
const answer = minimumBribesTransform(value.input);
12+
TEST_CASES.forEach((test) => {
13+
const answer = testingModule.minimumBribesText(test.input);
14+
testingModule.minimumBribes(test.input);
2815

2916
console.debug(
30-
`minimumBribesTransform(${value.input}) solution found: ${answer}`
17+
`minimumBribesText(${test.input}) solution found: ${answer}`
3118
);
3219

33-
expect(answer).toStrictEqual(value.expected);
20+
expect(answer).toStrictEqual(test.expected);
3421
});
3522
});
3623
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[
2+
{
3+
"title": "Test Case 0-0",
4+
"input": [2, 1, 5, 3, 4],
5+
"expected": "3"
6+
},
7+
{
8+
"title": "Test Case 0-1",
9+
"input": [2, 5, 1, 3, 4],
10+
"expected": "Too chaotic"
11+
},
12+
{
13+
"title": "Test Case 1-1",
14+
"input": [5, 1, 2, 3, 7, 8, 6, 4],
15+
"expected": "Too chaotic"
16+
},
17+
{
18+
"title": "Test Case 1-2",
19+
"input": [1, 2, 5, 3, 7, 8, 6, 4],
20+
"expected": "7"
21+
},
22+
{
23+
"title": "Test Case 2",
24+
"input": [1, 2, 5, 3, 4, 7, 8, 6],
25+
"expected": "4"
26+
}
27+
]

0 commit comments

Comments
 (0)