Skip to content

Instantly share code, notes, and snippets.

@cchoi34
Created January 16, 2020 15:27
Show Gist options
  • Save cchoi34/89949d381e482345e2b061b348e9fe94 to your computer and use it in GitHub Desktop.
Save cchoi34/89949d381e482345e2b061b348e9fe94 to your computer and use it in GitHub Desktop.

Rotate


Prompt

Write a function that takes a 2-Dimensional array of integers and returns a copy of that array rotated 90 degrees (clockwise). Are you able to do this in place? Note that these 2 Dimensional arrays will be squares, (N x N) with the same numbers of rows and columns.


Examples

let picture1 = [
  [1, 1, 1, 1],
  [0, 1, 1, 1],
  [0, 1, 1, 1],
  [0, 0, 0, 1],
]

let picture2 = [
  [1, 1, 1, 1],
  [1, 0, 1, 1],
  [0, 1, 0, 1],
  [1, 1, 0, 1],
]

rotate(picture1); // should return
/*  [
      [0, 0, 0, 1],
      [0, 1, 1, 1],
      [0, 1, 1, 1],
      [1, 1, 1, 1],
    ] */
  
rotate(picture2); // should return
 /* [
      [1, 0, 1, 1],
      [1, 1, 0, 1],
      [0, 0, 1, 1],
      [1, 1, 1, 1],
    ] */

Hints

Before you get started, write some examples of your own of different sizes of 2 Dimensional arrays. Is there any pattern when these get rotated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment