DEV Community

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

Posted on

1

Road to Genius: superior #50

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[m][n];
  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-i-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 = [
  [7,4,7],
  [9,6,4],
  [1,3,9]
]
rotate(M);
let A = M[2][0]

// A = ? (number)
Enter fullscreen mode Exit fullscreen mode

In today's challenge we'll be messing around with 2D matrices. There are no bugs to fix only a single answer is required.

After a quick look at the code, the function rotate reveals its nature, it's rotating an input matrix by 90°, as such:
coding challenge extra

If you don't trust me then you can take a piece of paper and work it out, in pseudo-code the rotation is as follows:

| 7 4 7 |              | 1 9 7 |
| 9 6 4 |  -- 90° -->  | 3 6 4 |
| 1 3 9 |              | 9 4 7 |
Enter fullscreen mode Exit fullscreen mode

Notice that i is the row, and j the column.
To find the value of A we need to get the number from the 2nd row and 0th column, which is 9 (bottom left corner).

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/

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)