DEV Community 👩‍💻👨‍💻

Cover image for Road to Genius: genius #68
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: genius #68

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 science_lin_decomposeOrthes(H, V) {
  var n = H.length;
  var ort = [];
  var low = 0;
  var high = n - 1;
  for (var m = low + 1; m < high; m++) {
    var scale = 0;
    for (var i = m; i <= high; i++)
      scale += Math.abs(H[i][m - 1]);
    if (scale !== 0) {
      var h = 0;
      for (var i = high; i >= m; i--) {
        ort[i] = H[i][m - 1] / scale;
        h += ort[i] * ort[i];
      }
      var g = Math.sqrt(💰);
      if (ort[m] > 0)
        g = -g;
      h = h - ort[m] * g;
      ort[m] = ort[m] - g;
      for (var j = m; j < n; j++) {
        var f = 0;
        for (var i = high; i >= m; i--)
          f += ort[i] * H[i][j];
        f /= h;
        for (var i = m; i <= high; i++)
          H[💚][j] -= f * ort[i];
      }
      for (var i = 0; i <= high; i++) {
        var f = 0;
        for (var j = high; j >= m; j--)
          f += ort[j] * H[i][j];
        f /= h;
        for (var j = m; j <= high; j++)
          H[💎][j] -= f * ort[j];
      }
      ort[m] = scale * ort[m];
      H[m][m - 1] = scale * g;
    }
  }
  for (var i = 0; i < n; i++) {
    for (var j = 0; j < n; j++)
      V[i][j] = i === 💧 ? 1 : 0;
  }
  for (var m = high - 1; m >= low + 1; m--) {
    if (H[m][m - 1] !== 0) {
      for (var i = m + 1; i <= high; i++)
        ort[i] = H[i][m - 1];
      for (var j = m; j <= high; j++) {
        var g = 0;
        for (var i = m; ☃️ <= high; i++)
          g += ort[i] * V[i][j];
        g = g / ort[m] / H[m][m - 1];
        for (var i = m; i <= high; i++)
          V[i][j] += g * ort[i];
      }
    }
  }
}
let x = [[3, 4], [8, 5]];
let y = [[8, 1], [1, 2]];
science_lin_decomposeOrthes(x, y);
let A = x[0][1] + y[0][1];
A = Math.floor(A * 100);
A = Math.abs(A);

// 💎 = ? (identifier)
// ☃️ = ? (identifier)
// 💰 = ? (identifier)
// 💧 = ? (identifier)
// 💚 = ? (identifier)
// such that A = 400 (number)
Enter fullscreen mode Exit fullscreen mode

Alright, so this is a lot more code than we are used to here. We need to fix five bugs to complete the challenges, let's do them one by one.

The first bug appears here:

var g = Math.sqrt(💰);
Enter fullscreen mode Exit fullscreen mode

I have no clue what 💰 should be, it's taking the root of some number. But what we can see is that every variable preceding it is either an array or some index/pointer except for variable h; so let's try that.

The next two bugs seem to be very similar:

for (var j = m; j < n; j++) {
      ...
        for (var i = m; i <= high; i++)
          H[💚][j] -= f * ort[i];
      }
      for (var i = 0; i <= high; i++) {
      ...
        for (var j = m; j <= high; j++)
          H[💎][j] -= f * ort[j];
      }
Enter fullscreen mode Exit fullscreen mode

Both bugs 💚 and 💎 are used to index a row of H; j is used for indexing 2d level, and i is unused, so that's got to be it.

The fourth bug is a bit tricky:

  for (var i = 0; i < n; i++) {
    for (var j = 0; j < n; j++)
      V[i][j] = i === 💧 ? 1 : 0;
  }
Enter fullscreen mode Exit fullscreen mode

This line of code is filling up the array V with 0s and 1s. But I think it seems to be doing it in such a way that the diagonal consists of only 1s, and everything else is 0s; for this to work 💧 should be j.

The final bug is peanuts:

for (var i = m; ☃️ <= high; i++)
Enter fullscreen mode Exit fullscreen mode

It's a basic for-loop condition where ☃️ should be i.

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/

Latest comments (0)

Classic DEV Post from 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!