From b0bcdb105af2eaa8a1a4adfcaf8340f34216921a Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 1 Aug 2024 12:41:26 -0400 Subject: [PATCH 1/2] =?UTF-8?q?[BUGFIX]=20[Hacker=20Rank]=20Interview=20Pr?= =?UTF-8?q?eparation=20Kit:=20Arrays:=20New=20Year=20Chaos.=20Sonarcloud?= =?UTF-8?q?=20issue=20javascript:S3800=20fixed=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor this function to always return the same type. --- .../interview_preparation_kit/arrays/new_year_chaos.js | 2 +- .../interview_preparation_kit/arrays/new_year_chaos.test.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) 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 cef5034a..f10f4f53 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.js +++ b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.js @@ -29,7 +29,7 @@ export function minimumBribes(q) { export function minimumBribesTransform(queue) { try { - return minimumBribes(queue); + return minimumBribes(queue).toString(10); } catch (e) { return 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 e1627d97..0d6201b3 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 @@ -4,7 +4,7 @@ import { logger as console } from '../../../logger.js'; import { minimumBribesTransform, TOO_CHAOTIC_ERROR } 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-0', input: [2, 1, 5, 3, 4], expected: '3' }, { title: 'Test Case 0-1', input: [2, 5, 1, 3, 4], @@ -15,8 +15,8 @@ const TEST_CASES = [ 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 } + { 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' } ]; describe('new_year_chaos', () => { From 48c29e3fd01a94f1cf10e5ce61e30e88f0a8f0e7 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 1 Aug 2024 12:59:24 -0400 Subject: [PATCH 2/2] =?UTF-8?q?[BUGFIX]=20[Hacker=20Rank]=20Interview=20Pr?= =?UTF-8?q?eparation=20Kit:=20Greedy=20Algorithms:=20Luck=20Balance.=20Son?= =?UTF-8?q?arcloud=20javascript:S2094=20issue=20fixed=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unexpected class with only a constructor. --- .../greedy_algorithms/luck-balance.js | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/greedy_algorithms/luck-balance.js b/src/hackerrank/interview_preparation_kit/greedy_algorithms/luck-balance.js index 9592b6f7..6ee80d19 100644 --- a/src/hackerrank/interview_preparation_kit/greedy_algorithms/luck-balance.js +++ b/src/hackerrank/interview_preparation_kit/greedy_algorithms/luck-balance.js @@ -29,19 +29,27 @@ class Contest { this.luck = luck; this.important = important; } + + getLuck() { + return this.luck; + } + + isImportant() { + return this.important !== 0; + } } export function luckBalance(k, contests) { - // Write your code here let importantContests = []; const nonimportantContests = []; - contests.forEach((contest) => { - const [luck, important] = [...contest]; - if (important === 1) { - importantContests.push(new Contest(luck, important)); + contests.forEach((contestData) => { + const [luck, important] = [...contestData]; + const contest = new Contest(luck, important); + if (contest.isImportant()) { + importantContests.push(contest); } else { - nonimportantContests.push(new Contest(luck, important)); + nonimportantContests.push(contest); } }); @@ -55,11 +63,11 @@ export function luckBalance(k, contests) { const cut = Math.min(k, size); for (let i = 0; i < cut; i++) { - total += importantContests[i].luck; + total += importantContests[i].getLuck(); } for (let i = cut; i < size; i++) { - total -= importantContests[i].luck; + total -= importantContests[i].getLuck(); } nonimportantContests.forEach((contest) => {