Skip to content

Commit

Permalink
question: separateAndSort
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelriosoliveira committed Sep 19, 2023
1 parent 0046270 commit 7b9e5a5
Show file tree
Hide file tree
Showing 4 changed files with 86 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)
- [317 - separateAndSort](src/2023/317-separateAndSort)
- [318 - buildStaircase](src/2023/318-buildStaircase)
</details>

Expand Down
21 changes: 21 additions & 0 deletions src/2023/317-separateAndSort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# separateAndSort

Interview question of the [issue #317 of rendezvous with cassidoo](https://buttondown.email/cassidoo/archive/people-who-think-they-know-everything-are-a-great/).

## The Question

**Given an array of integers, sort them into two separate sorted arrays of even and odd numbers.** If you see a zero, skip it.

Example:

```js
> separateAndSort([4,3,2,1,5,7,8,9])
> [[2,4,6], [1,3,5,7,9]]

> separateAndSort([1,1,1,1])
> [[], [1,1,1,1]]
```

## 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/317-separateAndSort/separateAndSort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { separateAndSort } from './separateAndSort';

describe('#separateAndSort', () => {
it('should separate and sort even and odd numbers', () => {
expect(separateAndSort([4, 3, 2, 1, 5, 7, 8, 9])).toStrictEqual([
[2, 4, 8],
[1, 3, 5, 7, 9],
]);
});

it('should handle an array with all even numbers', () => {
expect(separateAndSort([2, 4, 6, 8, 10])).toStrictEqual([[2, 4, 6, 8, 10], []]);
});

it('should handle an array with all odd numbers', () => {
expect(separateAndSort([1, 3, 5, 7, 9])).toStrictEqual([[], [1, 3, 5, 7, 9]]);
});

it('should handle an empty array', () => {
expect(separateAndSort([])).toStrictEqual([[], []]);
});

it('should handle an array with zeros and negative numbers', () => {
expect(separateAndSort([0, -2, 4, -6, 0, 3, 5, 7, 9])).toStrictEqual([
[-6, -2, 4],
[3, 5, 7, 9],
]);
});

it('should handle an array with only zeros', () => {
expect(separateAndSort([0, 0, 0])).toStrictEqual([[], []]);
});
});
31 changes: 31 additions & 0 deletions src/2023/317-separateAndSort/separateAndSort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Given an array of integers, sort them into two separate sorted arrays of even and odd numbers.
If you see a zero, skip it.
Example:
> separateAndSort([4,3,2,1,5,7,8,9])
> [[2,4,6], [1,3,5,7,9]]
> separateAndSort([1,1,1,1])
> [[], [1,1,1,1]]
*/

type EvensAndOdds = [number[], number[]];

export function separateAndSort(integers: number[]): EvensAndOdds {
return integers
.filter(Boolean)
.sort((a, b) => a - b)
.reduce<EvensAndOdds>(
([evens, odds], number) => {
if (number % 2 === 0) {
evens.push(number);
} else {
odds.push(number);
}
return [evens, odds];
},
[[], []],
);
}

0 comments on commit 7b9e5a5

Please sign in to comment.