Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rotate-Transform the Two-Dimensional Matrix in js

I have to create a function to rotate a two-dimensional matrix of N * N integer elements num times, where if num is positive, the rotation is clockwise, and if not, counterclockwise.

Examples

rotateTransform([
   [2, 4],
   [0, 0]
], 1) /*➞ [
   [0, 2],
   [0, 4]
]*/
rotateTransform([
   [2, 4],
   [0, 0]
], -1) /*➞ [
   [4, 0],
   [2, 0]
]*/

NOTE: I couldn’t even try…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

the same problem on edabit, and this was the top solution, IDK why you reposting it here…

function rotateTransform(arr, num) {
  if (num > 0)
    return rotateTransform(arr.map((_, i) => arr.map(x => x[i]).reverse()), num - 1);
  else if (num < 0)
    return rotateTransform(arr.map((_, i) => arr.map(x => x[i])).reverse(), num + 1);
  else
    return arr;
}

console.log(rotateTransform([
  [2, 4],
  [0, 0]
], 1))

console.log(rotateTransform([
  [2, 4],
  [0, 0]
], -1))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading