Skip to content

Commit 12bc714

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

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-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: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
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;
5+
const TOO_CHAOTIC_ERROR = 'Too chaotic';
6+
const NEW_YEAR_CHAOS_TOLERANCE = 2;
77

8-
export function minimumBribes(q: number[]): number {
8+
function minimumBribesCalculate(q: number[]): number {
99
let bribes = 0;
1010
let i = 0;
1111

@@ -15,7 +15,10 @@ export function minimumBribes(q: number[]): number {
1515
throw new Error(TOO_CHAOTIC_ERROR);
1616
}
1717

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

2023
fragment.forEach((k) => {
2124
if (k > value) {
@@ -28,11 +31,11 @@ export function minimumBribes(q: number[]): number {
2831
return bribes;
2932
}
3033

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

3437
try {
35-
result = minimumBribes(queue);
38+
result = `${minimumBribesCalculate(queue)}`;
3639
} catch (err: unknown) {
3740
if (err instanceof Error) {
3841
result = err.message;
@@ -42,4 +45,8 @@ export function minimumBribesTransform(queue: number[]): number | string {
4245
return result;
4346
}
4447

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

0 commit comments

Comments
 (0)