Skip to content

Commit

Permalink
question: flip
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelriosoliveira committed Jan 17, 2024
1 parent 5b1ab4a commit e82d236
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ I started this in the [issue #176](https://buttondown.email/cassidoo/archive/we-

- [333 - happyNewYear](src/2024/333-happyNewYear)
- [334 - letters](src/2024/334-letters)
- [335 - flip](src/2024/335-flip)
</details>

## Installing & Running
Expand Down
35 changes: 35 additions & 0 deletions src/2024/335-flip/README.md
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!
29 changes: 29 additions & 0 deletions src/2024/335-flip/flip.test.ts
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],
]);
});
});
52 changes: 52 additions & 0 deletions src/2024/335-flip/flip.ts
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)));
}

0 comments on commit e82d236

Please sign in to comment.