javascript编程题
by Priyabrata Biswas
通过Priyabrata Biswas
如何开始使用JavaScript进行竞争性编程 (How to get started with Competitive Programming in JavaScript)
If you’re not familiar with competitive programming, basically it is a mind sport with the aim of writing code to solve given problems. I was introduced to it in my freshman year by my seniors. As of this writing, I’m still not really great at it! Maybe it is due to the fact that I don’t like to code in C++, or maybe I am a lazy person who won’t take the time to actually learn it well enough. But, I like algorithms and data-structures as much as I like JavaScript!
如果您不熟悉竞争性编程 ,那么从根本上说,这是一项头脑运动,旨在编写代码来解决给定的问题。 大一时,我被大四学生介绍给我。 在撰写本文时,我仍然还不是很擅长! 也许是因为我不喜欢用C ++编写代码,或者我是一个懒惰的人,不会花时间去真正地学习它。 但是,我和JavaScript一样喜欢算法和数据结构!
So, the preposterous thought crossed my mind over and over again. ‘What if we start using JavaScript in the competitive arena?’ Turns out, this doesn’t seem like the uncharted territory that I thought it would be. Many platforms like HackerRank, CodeChef, and Codeforces support JavaScript.
因此,荒谬的想法一遍又一遍地打动了我。 “如果我们开始在竞争舞台上使用JavaScript怎么办?” 事实证明,这似乎不像我想象的那样是未知领域。 像HackerRank , CodeChef和Codeforces这样的许多平台都支持JavaScript。
I know C++ is a lot faster compared to JavaScript and can dynamically allocate memory. C and C++ are quite similar in terms of performance, but competitive programmers mostly use C++ because of its Standard Template Library (or STL). It provides common programming data structures like list, stack, array along with container classes, algorithms, and iterators out of the box.
我知道C ++与JavaScript相比要快得多,并且可以动态分配内存。 C和C ++在性能方面非常相似,但是具有竞争力的程序员大多使用C ++,因为它具有标准模板库 (或STL)。 它提供了开箱即用的通用编程数据结构,如列表,堆栈,数组以及容器类,算法和迭代器。
But, JavaScript offers something C++ lacks:
但是,JavaScript提供了C ++所缺少的东西:
Freedom!
自由!
Being a scripting language, JavaScript is inherently slow. But still, it is the most popular language out there. According to 2018’s Stack Overflow Developer Survey, 69.8% of respondents use JavaScript for their development purposes. But, at the same time, it doesn’t shine so well in the case of competitive programming. The reason is that it was simply not built for it!
作为一种脚本语言,JavaScript本质上是缓慢的。 但仍然是那里最受欢迎的语言。 根据2018年Stack Overflow开发人员调查 ,有69.8%的受访者出于开发目的而使用JavaScript。 但是,与此同时,在竞争性编程的情况下效果并不理想。 原因是它根本不是为它构建的!
Back in 1995, Brendan Eich developed JavaScript only for adding interactivity to web pages like handling a mouse click.
早在1995年, Brendan Eich就开发了JavaScript,其目的只是为了向网页添加交互性,例如处理鼠标单击。
Today, we can build servers, games, mobile applications, IoT applications and even machine learning in the browser is possible with JavaScript. So, why feel ashamed while using it in competitive programming?
今天,我们可以使用JavaScript来构建服务器,游戏,移动应用程序,IoT应用程序,甚至在浏览器中进行机器学习。 那么,为什么在竞争性编程中使用它时会感到as愧?
“Any application that can be written in JavaScript, will eventually be written in JavaScript.” — Jeff Atwood
“任何可以用JavaScript编写的应用程序,最终都将用JavaScript编写。” — 杰夫·阿特伍德
Remember what I told you about STL and the toolkit it provides for competitive programming? I thought to myself why doesn’t TC 39 come up with something similar for JavaScript!
还记得我告诉过您有关STL及其为竞争性编程提供的工具包的内容吗? 我心想,为什么TC 39不会针对JavaScript提出类似的建议!
Have you heard of ‘Node Package Manager’ also known as ‘npm’?
您是否听说过“ Node Package Manager”(也称为“ 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.
好吧,它是世界上最大的软件注册中心 ,拥有874,285多个软件包(截至撰写本文时),并且是Node.js的默认软件包管理器。
The idea is to develop an npm package much like C++’s STL
想法是开发类似于C ++的STL的npm软件包
介绍Mathball (Introducing Mathball)
Mathball is an npm package for competitive programming in JavaScript implementing optimized algorithms for faster execution. Okay, now I’m exaggerating! The truth is, it only supports 16 utility functions implementing brute-force approaches so far. I put together this tiny helper library for assisting people in competitive programming.
Mathball是一个npm软件包,用于JavaScript竞争性编程,实现优化算法以加快执行速度。 好吧,现在我太夸张了! 事实是,到目前为止,它仅支持16种实现蛮力方法的实用程序功能。 我整理了这个很小的帮助程序库,以帮助人们进行竞争性编程。
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:
如果在计算机上安装了Node.js和npm,则可以很容易地获得该软件包,方法是在终端中键入以下命令:
npm install mathball
You can access all the utilities via a mathball
object, M
, like so:
您可以通过mathball
对象M
来访问所有实用程序,如下所示:
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?
我应该如何在HackerRank或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 —
让我解释一下我的意思! 假设您正在尝试解决HackerRank上的这一特定问题-
Simple Array Sum | HackerRankCalculate the sum of integers in an array.www.hackerrank.com
简单数组总和| HackerRank 计算数组中整数的总和。 www.hackerrank.com
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.
不要被所有这些代码行淹没。 如果您以前使用过HackerRank,那么您已经知道它仅用于处理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:
首先,将以上内容复制并粘贴到index.js
文件中。 然后,在同一目录中,打开终端并键入:
npm install mathball
Next, inside the index.js
file, modify the following:
接下来,在index.js
文件中,修改以下内容:
Lastly, in order to bundle everything into a single file, I’m using Webpack but you’re free to choose any CommonJS module bundler!
最后,为了将所有内容捆绑到一个文件中,我正在使用Webpack,但您可以自由选择任何CommonJS模块捆绑器!
So, let’s create a webpack.config.js
file in the same directory with the following code in it:
因此,让我们在同一目录中创建一个webpack.config.js
文件,其中包含以下代码:
If you don’t already have Webpack installed, please install it like so:
如果尚未安装Webpack,请按照以下方式安装:
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!
现在,以上命令将创建一个名为bundle.js
的文件 在同一目录中。 因此,将其内容复制并粘贴到HackerRank上,然后单击* 提交代码 *。 而已!
结语 (Epilogue)
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.
一旦确定了问题所需的算法或数据结构,如果您拥有像Mathball这样的库就可以轻松进行编码。 另外,您不必在每次编写代码时都经过所有这些捆绑步骤。 这基本上是一次性的设置过程。 只需编码,然后将文件与最后一个命令捆绑在一起即可。
Fun fact — you can use Mathball in your project too!
有趣的事实 -您也可以在项目中使用Mathball!
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.
我将不断改进Mathball,并衷心欢迎您的贡献。 在一起,我们可以使Mathball做更多的事情! 这是仓库的链接 。
The purpose of this article was to evangelize the importance of competitive programming in the JavaScript community. I think learning algorithms and data structures prepares us to care more about the efficiency and complexity of our codebase. It makes us look twice for any memory leaks, and helps us to become better developers, in general.
本文的目的是宣传JavaScript社区中竞争性编程的重要性。 我认为学习算法和数据结构可以使我们更加关注代码库的效率和复杂性。 它使我们两次寻找任何内存泄漏,并通常帮助我们成为更好的开发人员。
Here’s a list of resources that inspired me to embark on my journey of supporting JavaScript in competitive programming:
这是启发我开始在竞争性编程中支持JavaScript的旅程的资源列表:
Pranay Dubey — JavaScript for Competitive Programming
Pranay Dubey-用于竞争性编程JavaScript
JavaScript for Algorithmic Competitive Programming
用于算法竞争性编程JavaScript
Thank you for your time! ✌️
感谢您的时间! ✌️
翻译自: https://www.freecodecamp.org/news/how-to-get-started-with-competitive-programming-in-javascript-76ad2e760efe/
javascript编程题