Skip to content

Commit

Permalink
question: rollCall
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelriosoliveira committed Dec 3, 2024
1 parent c049f15 commit ae9c443
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ I started this in the [issue #176](https://buttondown.email/cassidoo/archive/we-
- [368 - maxPairs](src/2024/368-maxPairs)
- [377 - groupAnagrams](src/2024/377-groupAnagrams)
- [378 - seeBuildingsLeft](src/2024/378-seeBuildingsLeft)
- [381 - rollCall](src/2024/381-rollCall)
</details>

## Installing & Running
Expand Down
24 changes: 24 additions & 0 deletions src/2024/381-rollCall/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# rollCall

Interview question of the [issue #381 of rendezvous with cassidoo](https://buttondown.com/cassidoo/archive/how-beautiful-it-is-to-get-up-and-go-do-something/).

## The Question

**Santa is conducting his daily roll call for the reindeer, but the printer has mistakenly printed all their names backwards. To take attendance properly, he urgently needs a tool to reverse the reindeer names and put them in alphabetical order! Can you help Santa?**

Example:

```js
rollCall(["yzneT","ydissaC","enimA"])
["Amine","Cassidy","Tenzy"]

rollCall(["rennuD","nexiV","recnarP","temoC","neztilB","recnaD","diduC","rehsaD","hploduR"])
["Blitzen","Comet","Cupid","Dancer","Dasher","Donner","Prancer","Rudolph","Vixen"]

rollCall(["A","B","C"])
["A","B","C"]
```

## Installing & Running

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

describe('#rollCall', () => {
it('should reverse names and sort them alphabetically', () => {
expect(rollCall(['yzneT', 'ydissaC', 'enimA'])).toStrictEqual(['Amine', 'Cassidy', 'Tenzy']);
});

it('should handle a list of all the reindeer names', () => {
expect(
rollCall([
'rennoD',
'nexiV',
'recnarP',
'temoC',
'neztilB',
'recnaD',
'dipuC',
'rehsaD',
'hploduR',
]),
).toStrictEqual([
'Blitzen',
'Comet',
'Cupid',
'Dancer',
'Dasher',
'Donner',
'Prancer',
'Rudolph',
'Vixen',
]);
});

it('should handle single-letter names without any change', () => {
expect(rollCall(['A', 'B', 'C'])).toStrictEqual(['A', 'B', 'C']);
});

it('should handle an empty list gracefully', () => {
expect(rollCall([])).toStrictEqual([]);
});

it('should handle a single name correctly', () => {
expect(rollCall(['adniL'])).toStrictEqual(['Linda']);
});

it('should handle names with mixed cases correctly', () => {
expect(rollCall(['AIz', 'aRiD', 'eMmA'])).toStrictEqual(['AmMe', 'DiRa', 'zIA']);
});

it('should handle duplicate reversed names and sort them properly', () => {
expect(rollCall(['adnaL', 'adnaL'])).toStrictEqual(['Landa', 'Landa']);
});

it('should handle names that are palindromes (no visible change after reversing)', () => {
expect(rollCall(['anna', 'bob', 'otto'])).toStrictEqual(['anna', 'bob', 'otto']);
});
});
22 changes: 22 additions & 0 deletions src/2024/381-rollCall/rollCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Santa is conducting his daily roll call for the reindeer,
but the printer has mistakenly printed all their names backwards.
To take attendance properly,
he urgently needs a tool to reverse the reindeer names and put them in alphabetical order!
Can you help Santa?
Example:
rollCall(["yzneT","ydissaC","enimA"])
["Amine","Cassidy","Tenzy"]
rollCall(["rennuD","nexiV","recnarP","temoC","neztilB","recnaD","diduC","rehsaD","hploduR"])
["Blitzen","Comet","Cupid","Dancer","Dasher","Donner","Prancer","Rudolph","Vixen"]
rollCall(["A","B","C"])
["A","B","C"]
*/

export function rollCall(names: string[]): string[] {
return names.map(name => name.split('').reverse().join('')).sort();
}

0 comments on commit ae9c443

Please sign in to comment.