Skip to content

Commit 4e17bcd

Browse files
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Arrays: New Year Chaos. Adjusted the interface to match what hackerrank expects.
1 parent 2905902 commit 4e17bcd

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from '@jest/globals';
22
import { logger as console } from '../../../logger';
33

4-
import { minimumBribesTransform } from './new_year_chaos';
4+
import nyc from './new_year_chaos';
55

66
import TEST_CASES from './new_year_chaos.testcases.json';
77

@@ -10,7 +10,8 @@ describe('new_year_chaos', () => {
1010
expect.assertions(5);
1111

1212
TEST_CASES.forEach((value) => {
13-
const answer = minimumBribesTransform(value.input);
13+
const answer = nyc.minimumBribesText(value.input);
14+
nyc.minimumBribes(value.input);
1415

1516
console.debug(
1617
`minimumBribesTransform(${value.input.toString()}) solution found: ${answer}`
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
[
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-
]
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+
]

src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md]]
33
*/
44

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

8-
export function minimumBribes(q: number[]): number {
6+
const TOO_CHAOTIC_ERROR = 'Too chaotic';
7+
const NEW_YEAR_CHAOS_TOLERANCE = 2;
8+
9+
function minimumBribesCalculate(q: number[]): number {
910
let bribes = 0;
1011
let i = 0;
1112

@@ -15,7 +16,10 @@ export function minimumBribes(q: number[]): number {
1516
throw new Error(TOO_CHAOTIC_ERROR);
1617
}
1718

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

2024
fragment.forEach((k) => {
2125
if (k > value) {
@@ -28,11 +32,11 @@ export function minimumBribes(q: number[]): number {
2832
return bribes;
2933
}
3034

31-
export function minimumBribesTransform(queue: number[]): number | string {
32-
let result: number | string = '';
35+
function minimumBribesText(queue: number[]): string {
36+
let result = '';
3337

3438
try {
35-
result = minimumBribes(queue);
39+
result = `${minimumBribesCalculate(queue)}`;
3640
} catch (err: unknown) {
3741
if (err instanceof Error) {
3842
result = err.message;
@@ -42,4 +46,8 @@ export function minimumBribesTransform(queue: number[]): number | string {
4246
return result;
4347
}
4448

45-
export default { minimumBribes, minimumBribesTransform, TOO_CHAOTIC_ERROR };
49+
function minimumBribes(q: number[]): void {
50+
console.log(minimumBribesText(q));
51+
}
52+
53+
export default { minimumBribes, minimumBribesText, TOO_CHAOTIC_ERROR };

0 commit comments

Comments
 (0)