Skip to content

Commit

Permalink
question: buildStaircase
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelriosoliveira committed Sep 19, 2023
1 parent 0ad35ee commit 0046270
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ I started this in the [issue #176](https://buttondown.email/cassidoo/archive/we-
- [314 - guessingGame](src/2023/314-guessingGame)
- [315 - countAndSay](src/2023/315-countAndSay)
- [316 - minSubs](src/2023/316-minSubs)
- [318 - buildStaircase](src/2023/318-buildStaircase)
</details>

## Installing & Running
Expand Down
25 changes: 25 additions & 0 deletions src/2023/318-buildStaircase/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# buildStaircase

Interview question of the [issue #318 of rendezvous with cassidoo](https://buttondown.email/cassidoo/archive/let-us-remember-that-our-voice-is-a-precious-gift/).

## The Question

**You have `n` equal-sized blocks and you want to build a staircase with them. Return the number of steps you can fully build.**

Example:

```js
> buildStaircase(6)
> 3

// #
// ##
// ###

> buildStaircase(9)
> 3 // it takes 10 blocks to make 4 steps
```

## Installing & Running

Just `pnpm i` to install all dependencies and then `pnpm t` to run the tests!
33 changes: 33 additions & 0 deletions src/2023/318-buildStaircase/buildStaircase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { buildStaircase } from './buildStaircase';

describe('#buildStaircase', () => {
it('should return 0 for n equal to 0', () => {
expect(buildStaircase(0)).toBe(0);
});

it('should return 1 for n less than 3', () => {
expect(buildStaircase(1)).toBe(1);
expect(buildStaircase(2)).toBe(1);
});

it('should return 2 for n less than 6', () => {
expect(buildStaircase(3)).toBe(2);
expect(buildStaircase(4)).toBe(2);
expect(buildStaircase(5)).toBe(2);
});

it('should return 3 for n less than 10', () => {
expect(buildStaircase(6)).toBe(3);
expect(buildStaircase(7)).toBe(3);
expect(buildStaircase(8)).toBe(3);
expect(buildStaircase(9)).toBe(3);
});

it('should return 4 for n less than 15', () => {
expect(buildStaircase(10)).toBe(4);
expect(buildStaircase(11)).toBe(4);
expect(buildStaircase(12)).toBe(4);
expect(buildStaircase(13)).toBe(4);
expect(buildStaircase(14)).toBe(4);
});
});
25 changes: 25 additions & 0 deletions src/2023/318-buildStaircase/buildStaircase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
You have `n` equal-sized blocks and you want to build a staircase with them.
Return the number of steps you can fully build.
Example:
> buildStaircase(6)
> 3
// #
// ##
// ###
> buildStaircase(9)
> 3 // it takes 10 blocks to make 4 steps
*/

export function buildStaircase(n: number): number {
let height = 2;
while (n > 0) {
n -= height;
height++;
}
return height - 2;
}

0 comments on commit 0046270

Please sign in to comment.