javascript编程题_如何开始使用JavaScript进行竞争性编程

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的旅程的资源列表:

  1. Pranay Dubey — JavaScript for Competitive Programming

    Pranay Dubey-用于竞争性编程JavaScript

  2. 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编程题

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392616.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hibernate Criteria(条件查询接口)

Criteria&#xff08;条件查询接口&#xff09; // 1.简单查询 List<Customer> list session.createCriteria(Customer.class).list();// 2.条件查询: Criteria criteria session.createCriteria(Customer.class); criteria.add(Restrictions.eq("name",&quo…

ElastciSearch简单总结(笔记)

前言&#xff1a; 前段时间在项目中使用了es,作为一个当前比较流行的分布式搜索引擎&#xff0c;在学习和使用它的过程中&#xff0c;踩了不少坑&#xff0c;这篇文章先简单整理了一下&#xff0c;后续会整理一下之前踩过的一些坑。 1. ElastciSearch是什么 ElasticSearch是一…

记一次ArrayList产生的线上OOM问题

前言&#xff1a;本以为(OutOfMemoryError)OOM问题会离我们很远&#xff0c;但在一次生产上线灰度的过程中就出现了Java.Lang.OutOfMemoryError:Java heap space异常&#xff0c;通过对线上日志的查看&#xff0c;最终定位到ArrayList#addAll方法中&#xff0c;出现这个问题的原…

leetcode 222. 完全二叉树的节点个数(dfs)

给出一个完全二叉树&#xff0c;求出该树的节点个数。说明&#xff1a;完全二叉树的定义如下&#xff1a;在完全二叉树中&#xff0c;除了最底层节点可能没填满外&#xff0c;其余每层节点数都达到最大值&#xff0c;并且最下面一层的节点都集中在该层最左边的若干位置。若最底…

css 计算属性的应用_如何使用一点CSS Grid魔术设计计算器应用

css 计算属性的应用by Deepika Gunda由Deepika Gunda 如何使用一点CSS Grid魔术设计计算器应用 (How to use a little CSS Grid magic to design a calculator app) This article is a quick intro to CSS Grid. We will be making a calculator using it.本文是CSS Grid的快速…

vc调试大全

一、调试基础 调试快捷键 F5&#xff1a; 开始调试 ShiftF5: 停止调试 F10&#xff1a; 调试到下一句&#xff0c;这里是单步跟踪 F11&#xff1a; 调试到下一句&#xff0c;跟进函数内部 ShiftF11: 从当前函数中跳出 CtrlF10: 调试到光标所在位置 F9&#xff1a; …

Google-Guava-EventBus源码解读

Guava是Google开源的一个Java基础类库&#xff0c;它在Google内部被广泛使用。Guava提供了很多功能模块比如&#xff1a;集合、并发库、缓存等&#xff0c;EventBus是其中的一个module&#xff0c;本篇结合EventBus源码来谈谈它的设计与实现。 概要 首先&#xff0c;我们先来预…

leetcode 1370. 上升下降字符串

给你一个字符串 s &#xff0c;请你根据下面的算法重新构造字符串&#xff1a; 从 s 中选出 最小 的字符&#xff0c;将它 接在 结果字符串的后面。 从 s 剩余字符中选出 最小 的字符&#xff0c;且该字符比上一个添加的字符大&#xff0c;将它 接在 结果字符串后面。 重复步骤…

mysql 设置事物自动提交_mysql事务自动提交的问题

1&#xff1a;mysql的aut0commit配置默认是开启的&#xff0c;也就是没执行一条sql都会提交一次&#xff0c;就算显示的开启事务也会导致多条SQL不在一个事务中&#xff0c;如果需要相关的SQL在同一个事务中执行&#xff0c;那么必须将autocommit设置为OFF&#xff0c;再显式开…

rest laravel_如何通过测试驱动开发来构建Laravel REST API

rest laravelby Kofo Okesola由Kofo Okesola 如何通过测试驱动开发来构建Laravel REST API (How to build a Laravel REST API with Test-Driven Development) There is a famous quote by James Grenning, one of the pioneers in TDD and Agile development methodologies:T…

python之numpy

numpy是一个多维的数组对象&#xff0c;类似python的列表&#xff0c;但是数组对象的每个元素之间由空格隔开。 一、数组的创建 1.通过numpy的array(参数)&#xff0c;参数可以是列表、元组、数组、生成器等 由arr2和arr3看出&#xff0c;对于多维数组来说&#xff0c;如果最里…

git 上传

转载于:https://www.cnblogs.com/benbentu/p/6543154.html

Liferay 部署war包时候的deployDirectory 细节分析

引入&#xff1a; 在上文中&#xff0c;我们从宏观上讲解了Liferay部署war包的动作是如何触发监听器并且完成部署过程的&#xff0c;但是其中最核心的一块deployDirectory我们没讲&#xff0c;它的作用是当有了临时目录并且已经把war包的内容展开到该目录之后&#xff0c;是如何…

leetcode 164. 最大间距(桶排序)

给定一个无序的数组&#xff0c;找出数组在排序之后&#xff0c;相邻元素之间最大的差值。 如果数组元素个数小于 2&#xff0c;则返回 0。 示例 1: 输入: [3,6,9,1] 输出: 3 解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。 示例 2: …

批处理定时mysql备份数据库_定时备份mysql数据库的批处理

定时备份mysql数据库的批处理代码&#xff0c;保存为backup_mysql.bat&#xff0c;运行即可。复制代码 代码如下:echo offset txt1%date:~0,4%::当前年set txt2%date:~5,2%::当前月set txt3%date:~8,2%::当前日set txt4%time:~0,2%::当前小时set txt5%time:~3,2%::当前分钟set …

算法训练营 重编码_您在编码训练营期间可能面临的最大挑战

算法训练营 重编码by Joanna Gaudyn乔安娜高登(Joanna Gaudyn) 您在编码训练营期间可能面临的最大挑战 (The biggest struggles you might face during a coding bootcamp) You think that during a coding bootcamp nothing can be more challenging than learning programmi…

1449 砝码称重(思维)

题目链接&#xff1a;https://www.51nod.com/onlineJudge/submitDetail.html#!judgeId259281 题解&#xff1a;这题有一个技巧&#xff0c;毕竟是w^0,w^1,w^2....这样&#xff0c;必然会想到w进制&#xff0c;而且就只能用一次。 那么就简单了&#xff0c;把m拆成w进制&#xf…

leetcode 454. 四数相加 II(哈希表)

给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) &#xff0c;使得 A[i] B[j] C[k] D[l] 0。 为了使问题简单化&#xff0c;所有的 A, B, C, D 具有相同的长度 N&#xff0c;且 0 ≤ N ≤ 500 。所有整数的范围在 -228 到 228 - 1 之间&#xf…

“换标”Intel的穷则思变

成语有云“穷则思变”&#xff0c;用这个词来形容早先的Intel换标也最恰当不过。当然这里“穷”&#xff0c;不是说Intel很贫穷&#xff0c;而是说Intel在自己的产业到了尽头。Intel推产品概念的水平是一流的&#xff0c;虽然某些概念事后被认为是错误的&#xff08;如&#xf…

mysql开发中遇到的坑_mysql优化过程中遇见的坑(mysql优化问题特别注意)

单条查询最后添加 LIMIT 1&#xff0c;停止全表扫描。对于char(4) 或者vachar(4)&#xff0c;无论是中文还是英文都是存储四个字符&#xff0c;注意是字符而不是字节。如果一个字段未int类型&#xff0c;此类型只有0、1两个状态&#xff0c;需要为此建立索引吗&#xff1f;过度…