-
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
5b1ab4a
commit e82d236
Showing
4 changed files
with
117 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,35 @@ | ||
# flip | ||
|
||
Interview question of the [issue #335 of rendezvous with cassidoo](https://buttondown.email/cassidoo/archive/try-and-fail-but-dont-fail-to-try-john-quincy/). | ||
|
||
## The Question | ||
|
||
**Given a 2D array, write a function that flips it vertically or horizontally.** | ||
|
||
Example: | ||
|
||
```js | ||
let array = [ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
[7, 8, 9] | ||
] | ||
|
||
> flip(array, 'horizontal') | ||
> [ | ||
[3, 2, 1], | ||
[6, 5, 4], | ||
[9, 8, 7] | ||
] | ||
|
||
> flip(array, 'vertical') | ||
> [ | ||
[7, 8, 9], | ||
[4, 5, 6], | ||
[1, 2, 3] | ||
] | ||
``` | ||
|
||
## 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,29 @@ | ||
import { flip } from './flip'; | ||
|
||
describe('#flip', () => { | ||
it('should flip the array horizontally', () => { | ||
const array = [ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
[7, 8, 9], | ||
]; | ||
expect(flip(array, 'horizontal')).toStrictEqual([ | ||
[3, 2, 1], | ||
[6, 5, 4], | ||
[9, 8, 7], | ||
]); | ||
}); | ||
|
||
it('should flip the array vertically', () => { | ||
const array = [ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
[7, 8, 9], | ||
]; | ||
expect(flip(array, 'vertical')).toStrictEqual([ | ||
[7, 8, 9], | ||
[4, 5, 6], | ||
[1, 2, 3], | ||
]); | ||
}); | ||
}); |
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,52 @@ | ||
/* | ||
Given a 2D array, write a function that flips it vertically or horizontally. | ||
Example: | ||
let array = [ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
[7, 8, 9] | ||
] | ||
> flip(array, 'horizontal') | ||
> [ | ||
[3, 2, 1], | ||
[6, 5, 4], | ||
[9, 8, 7] | ||
] | ||
> flip(array, 'vertical') | ||
> [ | ||
[7, 8, 9], | ||
[4, 5, 6], | ||
[1, 2, 3] | ||
] | ||
*/ | ||
|
||
type Matrix = number[][]; | ||
|
||
function flipHorizontally(matrix: Matrix): Matrix { | ||
return matrix.map(row => row.toReversed()); | ||
} | ||
|
||
function transpose(matrix: Matrix) { | ||
const transposed: Matrix = []; | ||
for (let i = 0; i < matrix.length; i++) { | ||
const column: number[] = []; | ||
|
||
for (let j = 0; j < matrix[i].length; j++) { | ||
column.push(matrix[j][i]); | ||
} | ||
|
||
transposed.push(column); | ||
} | ||
return transposed; | ||
} | ||
|
||
export function flip(matrix: Matrix, direction: 'horizontal' | 'vertical'): Matrix { | ||
if (direction === 'horizontal') { | ||
return flipHorizontally(matrix); | ||
} | ||
return transpose(flipHorizontally(transpose(matrix))); | ||
} |