DEV Community

Cover image for Road to Genius: superior #66
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: superior #66

Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.

function swap(arr, [i, j], [m, n]) {
  const temp = arr[i][j];
  arr[i][j] = arr[🍎][πŸ’°];
  arr[m][n] = temp;
}
function rotate(M) {
  const n = M.length;
  for (let i = 0; i < n - 1; i++) {
    for (let j = 0; j < n - i; j++) {
      swap(M, [i, j], [n - j - 1, n - πŸ’Ž - 1]);
    }
  }
  for (let i = 0; i < n / 2; i++) {
    for (let j = 0; j < n; j++) {
      swap(M, [i, j], [n - i - 1, j]);
    }
  }
}
let M = [[2, 7, 1], [4, 2, 9], [8, 7, 3]];
rotate(M);
let A = M[2][1];

// πŸ’Ž = ? (identifier)
// πŸ’° = ? (identifier)
// 🍎 = ? (identifier)
// such that A = 9 (number)
Enter fullscreen mode Exit fullscreen mode

Our good friend rotate is back again, if you remember this function rotates a matrix by 90Β° clockwise. This time we have to fix three bugs to proceed.

The first two bugs appear on the same line within the function swap. This function swaps two elements at i,j with m,n. Knowing this we know that 🍎 and πŸ’° should be m and n respectively.

The final and third bug appears here:

swap(M, [i, j], [n - j - 1, n - πŸ’Ž - 1]);
Enter fullscreen mode Exit fullscreen mode

This calls the function swap on i,j to be swapped with n-j-1, n-i-1 and is critical for a correct rotation.

coding challenge answer

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Get started and become a certified Codr today at https://nevolin.be/codr/

Top comments (0)