-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0046270
commit 7b9e5a5
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([[], []]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}, | ||
[[], []], | ||
); | ||
} |