Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.7 Rotate Matrix #7

Open
jtkaufman737 opened this issue Dec 4, 2018 · 2 comments
Open

1.7 Rotate Matrix #7

jtkaufman737 opened this issue Dec 4, 2018 · 2 comments

Comments

@jtkaufman737
Copy link

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?

@jtkaufman737
Copy link
Author

jtkaufman737 commented Dec 4, 2018

let array = [
  [0,1,2,3],
  [0,1,2,3],
  [0,1,2,3],
  [0,1,2,3]
]

let array2 = [
  ['a','a','a','a','a'],
  ['b','b','b','b','b'],
  ['c','c','c','c','b'],
  ['d','d','d','d','d'],
  ['e','e','e','e','e']
]

function pixelPerfect(arr) {
  let result = [];

  for(let i = 0; i < arr.length; i++) {
    result.push([]); // probably a clever way to wrap this into the other loop...
  }

  for(let i=0; i < arr.length; i++) {
    for(let j = arr.length - 1; j >= 0; j --) {
      result[j][i] = arr[i][j];
    }
  }

  console.log(arr);
  console.log(result);
}

pixelPerfect(array);
pixelPerfect(array2);

  /* 
    0 [  0,  1,  2,  3],      
    1 [  0,  1,  2,  3],
    2 [  0,  1,  2,  3],
    3 [  0,  1,  2,  3],

    0 [  0,  0,  0,  0],
    1 [  1,  1,  1,  1],
    2 [  2,  2,  2,  2],
    3 [  3,  3,  3,  3]

    3[3] => 0[3]           // when I got to this point I started looking at what increments together
    2[3] => 0[2]           // (last and first numbers) and then the inner two numbers 
    1[3] => 0[1]           // increment opposite of one another
    0[3] => 0[0]

    3[2] => 1[3]
    2[2] => 1[2]
    1[2] => 1[1]
    0[2] => 1[0]

    3[1] => 2[3]
    2[1] => 2[2]
    1[1] => 2[1]
    0[1] => 2[0]

    3[0] => 3[3]
    2[0] => 3[2]
    1[0] => 3[1]
    0[0] => 3[0]
  */

@lpatmo
Copy link

lpatmo commented Dec 4, 2018

https://repl.it/@lpatmo/QuizzicalCoarseResource

function createMatrix(n) {
  let newMatrix = []
  for (let row = 0; row < n; row++) {
      newMatrix.push([])
    for (let column = 0; column < n; column++) { 
    }
  }
  console.log('new matrix:', newMatrix)
  return newMatrix;
}

function rotateMatrix(matrix) {
  //find the n of the matrix
  let n = matrix.length;
  let rotated = createMatrix(n);
  for (let i = n-1; i >= 0; i-- ) {
     for (let j = 0; j < n; j++) {
       console.log(i, j)
       rotated[j].push(matrix[i][j])
     }
  }
  console.log('rotated:', rotated) 
}
console.log(rotateMatrix([[1,2,3],[4,5,6],[7,8,9]]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants