-
Notifications
You must be signed in to change notification settings - Fork 2
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
Comments
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]
*/ |
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
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?
The text was updated successfully, but these errors were encountered: