Skip to content

Commit

Permalink
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Recursion: Davis'…
Browse files Browse the repository at this point in the history
… Staircase. Problem rewritten as OOP to avoid "no-param-reassign". Clean code: better test cases data naming.
  • Loading branch information
Gonzalo Diaz committed Aug 9, 2024
1 parent 48a3785 commit b60f163
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { describe, expect, it } from '@jest/globals';
import { logger as console } from '../../../logger';

import {
stepPerms,
step_perms_comput_with_cache
} from './ctci_recursive_staircase';
import { stepPerms, StepPerms } from './ctci_recursive_staircase';
import TEST_CASES from './ctci_recursive_staircase.testcases.json';
import TEST_CASES_GENERALIZED from './ctci_recursive_staircase_generalized.testcases.json';

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

TEST_CASES.forEach((testSet) => {
testSet?.tests.forEach((test) => {
const answer = stepPerms(test.input);
const answer = stepPerms(test.n_steps);

console.debug(`stepPerms(${test.input}) solution found: ${answer}`);
console.debug(`stepPerms(${test.n_steps}) solution found: ${answer}`);

expect(answer).toStrictEqual(test.expected);
});
Expand All @@ -26,17 +23,16 @@ describe('ctci_recursive_staircase', () => {
it('step_perms_comput_with_cache test cases', () => {
expect.assertions(3);

const TOP_LIMIT = 10 ** 10 + 7;

TEST_CASES_GENERALIZED.forEach((testSet) => {
testSet?.tests.forEach((test) => {
const initial_cache: Record<number, number> = {};
const answer = step_perms_comput_with_cache(
test.input,
initial_cache,
test.limit
);
const stairs = new StepPerms(TOP_LIMIT, test.steps_limit);

const answer = stairs.step_perms_comput_with_cache(test.n_steps);

console.debug(
`step_perms_comput_with_cache(${test.input}, ${initial_cache}, ${test.limit}) solution found: ${answer}`
`step_perms_comput_with_cache(${test.n_steps}, ${test.steps_limit}) solution found: ${answer}`
);

expect(answer).toStrictEqual(test.expected);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
[
{
"title": "Sample Test case 0",
"tests": [
{
"input": 1,
"expected": 1
},
{
"input": 3,
"expected": 4
},
{
"input": 7,
"expected": 44
}
]
},
{
"title": "Sample Test case 9",
"tests": [
{
"input": 5,
"expected": 13
},
{
"input": 8,
"expected": 81
}
]
},
{
"title": "Sample Test case 10",
"tests": [
{
"input": 15,
"expected": 5768
},
{
"input": 20,
"expected": 121415
},
{
"input": 27,
"expected": 8646064
}
]
}
{
"title": "Sample Test case 0",
"tests": [
{
"n_steps": 1,
"expected": 1
},
{
"n_steps": 3,
"expected": 4
},
{
"n_steps": 7,
"expected": 44
}
]
},
{
"title": "Sample Test case 9",
"tests": [
{
"n_steps": 5,
"expected": 13
},
{
"n_steps": 8,
"expected": 81
}
]
},
{
"title": "Sample Test case 10",
"tests": [
{
"n_steps": 15,
"expected": 5768
},
{
"n_steps": 20,
"expected": 121415
},
{
"n_steps": 27,
"expected": 8646064
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,41 @@
const TOP_LIMIT = 10 ** 10 + 7;
const STEPS_LIMIT = 3;

export function step_perms_comput_with_cache(
n_steps: number,
cache: Record<number, number>,
steps_limit: number
): number {
if (0 <= n_steps && n_steps <= 2) {
return n_steps;
export class StepPerms {
TOP_LIMIT = 1;
STEPS_LIMIT = 1;
CACHE: Record<number, number> = {};

constructor(top_limit: number, steps_limit: number) {
this.TOP_LIMIT = top_limit;
this.STEPS_LIMIT = steps_limit;
}

const keys = new Set(Object.values(cache));
let result = 0;

for (let i = 1; i <= Math.min(steps_limit, n_steps); i++) {
const searchKey = n_steps - i;
if (!keys.has(searchKey)) {
cache[searchKey] = step_perms_comput_with_cache(
searchKey,
cache,
steps_limit
);
step_perms_comput_with_cache(n_steps: number): number {
if (0 <= n_steps && n_steps <= 2) {
return n_steps;
}

result += cache[searchKey];
}
const keys = new Set(Object.keys(this.CACHE));
let result = 0;

for (let i = 1; i <= Math.min(this.STEPS_LIMIT, n_steps); i++) {
const searchKey = n_steps - i;
if (!keys.has(searchKey.toString())) {
this.CACHE[searchKey] = this.step_perms_comput_with_cache(searchKey);
}

result += this.CACHE[searchKey];
}

return result + (n_steps <= steps_limit ? 1 : 0);
return result + (n_steps <= this.STEPS_LIMIT ? 1 : 0);
}
}

export function stepPerms(n: number): number {
const initial_cache: Record<number, number> = {};
return (
step_perms_comput_with_cache(n, initial_cache, STEPS_LIMIT) % TOP_LIMIT
);
const stairs = new StepPerms(TOP_LIMIT, STEPS_LIMIT);

return stairs.step_perms_comput_with_cache(n) % TOP_LIMIT;
}

export default { stepPerms, step_perms_comput_with_cache };
export default { stepPerms, StepPerms };
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"title": "Own sample 1",
"tests": [
{
"input": 4,
"limit": 3,
"n_steps": 4,
"steps_limit": 3,
"expected": 7
}
]
Expand All @@ -13,8 +13,8 @@
"title": "Own sample 2",
"tests": [
{
"input": 5,
"limit": 4,
"n_steps": 5,
"steps_limit": 4,
"expected": 15
}
]
Expand All @@ -23,8 +23,8 @@
"title": "Own sample 3",
"tests": [
{
"input": 6,
"limit": 2,
"n_steps": 6,
"steps_limit": 2,
"expected": 13
}
]
Expand Down

0 comments on commit b60f163

Please sign in to comment.