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編程題