Created
March 29, 2018 21:36
-
-
Save michaelwclark/fa6ec931791ef935a0559c6216e5c7bb to your computer and use it in GitHub Desktop.
Rotate 2D Array
This file contains hidden or 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
| export default const rotate2dArray = array => { | |
| const resultLength = array[0].length | |
| const resultArray = Array(resultLength).fill([]) | |
| return resultArray.map((x, widthIndex) => | |
| array.map(innerArray => innerArray[widthIndex]) | |
| ) | |
| } |
This file contains hidden or 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
| import rotate2dArray from './rotate2dArray' | |
| describe('rotate2dArray', () => { | |
| it('should rotate array 90 degrees', () => { | |
| const input = [[1, 2, 3], [4, 5, 6]] | |
| const expected = [[1, 4], [2, 5], [3, 6]] | |
| expect(rotate2dArray(input)).toEqual(expected) | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment