DEV Community

Cover image for Road to Genius: advanced #43
Ilya Nevolin
Ilya Nevolin

Posted on

2 1

Road to Genius: advanced #43

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 RPN(tokens) {
  const stack = [];
  for (let index = 0; index < tokens.length; index++) {
    const token = tokens[index];
    if (!Number.isNaN(Number(token))) {
      stack.push(token);
    } else {
      const a = Number(stack.pop());
      const b = Number(stack.pop());
      if (token === '*') {
        stack.push(b * a);
      } else if (token === '/') {
        stack.push(b / a);
      } else if (token === '+') {
        stack.push(b + a);
      } else if (token === 🍎) {
        stack.push(b - a);
      }
    }
  }
  return stack.pop();
}
let arr = ['5', '4', '1', '2', '+', '7', '*', '/', '*', '2', '+', '4', '+'];
let A = RPN(arr);
A = Math.floor(A);

// 🍎 = ? (string)
// such that A = 6 (number)
Enter fullscreen mode Exit fullscreen mode

Here's an interesting challenge where we only have to fix one bug 🍎. It's an easy challenge. To solve it we have to look at the environment surrounding the bug:

if (token === '*') {
  stack.push(b * a);
} else if (token === '/') {
  stack.push(b / a);
} else if (token === '+') {
  stack.push(b + a);
} else if (token === 🍎) {
  stack.push(b - a);
}
Enter fullscreen mode Exit fullscreen mode

Notice that the token is some string that represents an operation (*, /, + and 🍎), the push operation below it reveals its actual operation, so 🍎 has to be -.

coding challenge answer

A bit more info about this code; The function RPN stands for Reverse Polish Notation, it's a way of representing a series of mathematical operations to be computed. You can Google it for more details, since we might encounter it again later I'll leave it at this.

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/

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay