Skip to content

Commit 0fb9094

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Recursion: Davis' Staircase. Problem rewritten as OOP to avoid "no-param-reassign". Clean code: better test cases data naming.
1 parent 0bf5dc2 commit 0fb9094

File tree

4 files changed

+91
-76
lines changed

4 files changed

+91
-76
lines changed

src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.js

+30-16
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,45 @@
44
*/
55

66
const TOP_LIMIT = 10 ** 10 + 7;
7-
const STEPS_LIMIT = 3;
8-
const CACHE = {};
7+
const STEPSLIMIT = 3;
98

10-
export function stepPermsComputWithCache(nSteps, stepsLimit) {
11-
if (nSteps >= 0 && nSteps <= 2) {
12-
return nSteps;
9+
export class StepPerms {
10+
TOP_LIMIT = 1;
11+
12+
STEPS_LIMIT = 1;
13+
14+
CACHE = {};
15+
16+
constructor(topLimit, stepsLimit) {
17+
this.TOP_LIMIT = topLimit;
18+
this.STEPS_LIMIT = stepsLimit;
1319
}
1420

15-
const keys = new Set(Object.keys(CACHE));
16-
let result = 0;
21+
stepPermsComputWithCache(nSteps) {
22+
if (nSteps >= 0 && nSteps <= 2) {
23+
return nSteps;
24+
}
25+
26+
const keys = new Set(Object.keys(this.CACHE));
27+
let result = 0;
28+
29+
for (let i = 1; i <= Math.min(this.STEPS_LIMIT, nSteps); i++) {
30+
const searchKey = nSteps - i;
31+
if (!keys.has(searchKey.toString())) {
32+
this.CACHE[searchKey] = this.stepPermsComputWithCache(searchKey);
33+
}
1734

18-
for (let i = 1; i <= Math.min(stepsLimit, nSteps); i++) {
19-
const searchKey = nSteps - i;
20-
if (!keys.has(searchKey)) {
21-
CACHE[searchKey] = stepPermsComputWithCache(searchKey, stepsLimit);
35+
result += this.CACHE[searchKey];
2236
}
2337

24-
result += CACHE[searchKey];
38+
return result + (nSteps <= this.STEPS_LIMIT ? 1 : 0);
2539
}
26-
27-
return result + (nSteps <= stepsLimit ? 1 : 0);
2840
}
2941

3042
export function stepPerms(n) {
31-
return stepPermsComputWithCache(n, STEPS_LIMIT) % TOP_LIMIT;
43+
const stairs = new StepPerms(TOP_LIMIT, STEPSLIMIT);
44+
45+
return stairs.stepPermsComputWithCache(n) % TOP_LIMIT;
3246
}
3347

34-
export default { stepPerms, stepPermsComputWithCache };
48+
export default { stepPerms, StepPerms };

src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase.test.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { describe, expect, it } from '@jest/globals';
22
import { logger as console } from '../../../logger.js';
33

4-
import {
5-
stepPerms,
6-
stepPermsComputWithCache
7-
} from './ctci_recursive_staircase.js';
4+
import { stepPerms, StepPerms } from './ctci_recursive_staircase.js';
85
import TEST_CASES from './ctci_recursive_staircase.testcases.json';
96
import TEST_CASES_GENERALIZED from './ctci_recursive_staircase_generalized.testcases.json';
107

@@ -14,9 +11,9 @@ describe('ctci_recursive_staircase', () => {
1411

1512
TEST_CASES.forEach((testSet) => {
1613
testSet?.tests.forEach((test) => {
17-
const answer = stepPerms(test.input);
14+
const answer = stepPerms(test.n_steps);
1815

19-
console.debug(`stepPerms(${test.input}) solution found: ${answer}`);
16+
console.debug(`stepPerms(${test.n_steps}) solution found: ${answer}`);
2017

2118
expect(answer).toStrictEqual(test.expected);
2219
});
@@ -26,9 +23,13 @@ describe('ctci_recursive_staircase', () => {
2623
it('stepPermsComputWithCache test cases', () => {
2724
expect.assertions(3);
2825

26+
const TOP_LIMIT = 10 ** 10 + 7;
27+
2928
TEST_CASES_GENERALIZED.forEach((testSet) => {
3029
testSet?.tests.forEach((test) => {
31-
const answer = stepPermsComputWithCache(test.input, test.limit);
30+
const stairs = new StepPerms(TOP_LIMIT, test.steps_limit);
31+
32+
const answer = stairs.stepPermsComputWithCache(test.n_steps);
3233

3334
console.debug(
3435
`stepPermsComputWithCache(${test.input}, ${test.limit}) solution found: ${answer}`
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
[
2-
{
3-
"title": "Sample Test case 0",
4-
"tests": [
5-
{
6-
"input": 1,
7-
"expected": 1
8-
},
9-
{
10-
"input": 3,
11-
"expected": 4
12-
},
13-
{
14-
"input": 7,
15-
"expected": 44
16-
}
17-
]
18-
},
19-
{
20-
"title": "Sample Test case 9",
21-
"tests": [
22-
{
23-
"input": 5,
24-
"expected": 13
25-
},
26-
{
27-
"input": 8,
28-
"expected": 81
29-
}
30-
]
31-
},
32-
{
33-
"title": "Sample Test case 10",
34-
"tests": [
35-
{
36-
"input": 15,
37-
"expected": 5768
38-
},
39-
{
40-
"input": 20,
41-
"expected": 121415
42-
},
43-
{
44-
"input": 27,
45-
"expected": 8646064
46-
}
47-
]
48-
}
2+
{
3+
"title": "Sample Test case 0",
4+
"tests": [
5+
{
6+
"n_steps": 1,
7+
"expected": 1
8+
},
9+
{
10+
"n_steps": 3,
11+
"expected": 4
12+
},
13+
{
14+
"n_steps": 7,
15+
"expected": 44
16+
}
17+
]
18+
},
19+
{
20+
"title": "Sample Test case 9",
21+
"tests": [
22+
{
23+
"n_steps": 5,
24+
"expected": 13
25+
},
26+
{
27+
"n_steps": 8,
28+
"expected": 81
29+
}
30+
]
31+
},
32+
{
33+
"title": "Sample Test case 10",
34+
"tests": [
35+
{
36+
"n_steps": 15,
37+
"expected": 5768
38+
},
39+
{
40+
"n_steps": 20,
41+
"expected": 121415
42+
},
43+
{
44+
"n_steps": 27,
45+
"expected": 8646064
46+
}
47+
]
48+
}
4949
]

src/hackerrank/interview_preparation_kit/recursion_and_backtracking/ctci_recursive_staircase_generalized.testcases.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"title": "Own sample 1",
44
"tests": [
55
{
6-
"input": 4,
7-
"limit": 3,
6+
"n_steps": 4,
7+
"steps_limit": 3,
88
"expected": 7
99
}
1010
]
@@ -13,8 +13,8 @@
1313
"title": "Own sample 2",
1414
"tests": [
1515
{
16-
"input": 5,
17-
"limit": 4,
16+
"n_steps": 5,
17+
"steps_limit": 4,
1818
"expected": 15
1919
}
2020
]
@@ -23,8 +23,8 @@
2323
"title": "Own sample 3",
2424
"tests": [
2525
{
26-
"input": 6,
27-
"limit": 2,
26+
"n_steps": 6,
27+
"steps_limit": 2,
2828
"expected": 13
2929
}
3030
]

0 commit comments

Comments
 (0)