by Priyabrata Biswas
Have you heard of ‘Node Package Manager’ also known as ‘npm’?
Well, it’s the world’s largest software registry with over 874,285 packages (as of this writing) and is the default package manager for Node.js.
The idea is to develop an npm package much like C++’s STL
You can get the package quite easily if you have Node.js and npm installed on your machine by typing the following command in your terminal:
npm install mathball
You can access all the utilities via a
M, like so:
Again, just getting an individual function is as easy as this:
Oh, now you must be thinking —
How am I supposed to use a third-party library inside a platform like HackerRank or CodeChef?
The answer is simple, just bundle it! 😎
Let me explain what I mean! Let’s say you are trying to solve this particular problem on HackerRank —
Don’t be overwhelmed by all those lines of code. If you’ve used HackerRank before then you already know that it’s just for handling I/O.
First, copy and paste the above contents in a file,
index.js. Then, in the same directory, open up the terminal and type:
npm install mathball
Next, inside the
index.js file, modify the following:
Lastly, in order to bundle everything into a single file, I’m using Webpack but you’re free to choose any CommonJS module bundler!
So, let’s create a
webpack.config.js file in the same directory with the following code in it:
If you don’t already have Webpack installed, please install it like so:
npm install -g webpack webpack-cli
Finally, type the following:
webpack --config ./webpack.config.js --mode=development
Now, the above command will create a file named
bundle.js in the same directory. So, copy and paste its content on HackerRank and hit *Submit Code*. That’s it!
It doesn’t make sense to go through all that nonsense just so you can compute the sum of an array…right? So I must confess that problems on competitive programming tend to be far more complicated than this.
I believe competitive programming is more about figuring out ways to solve a problem than just solving them!
Once you figure out what algorithm or data structure your problem needs, coding becomes quite easy if you have a library like Mathball at your disposal. Also, you don’t have to go through all those steps for bundling every time you code something. It’s basically a one-time setup process. Just code along, and bundle your files with the last command.
Fun fact — you can use Mathball in your project too!
I will be constantly improving Mathball and I sincerely welcome your contribution. Together, we can make Mathball do so much more! Here’s the link for the repo.
Thank you for your time! ✌️