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.pswp.cn/news/392616.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392616.shtml
英文地址,請注明出處:http://en.pswp.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;過度…