5
3

More than 1 year has passed since last update.

【JavaScript】ローカルでpaizaの問題を解くための環境構築

Last updated at Posted at 2021-12-11

paizaのスキルチェックのような標準入力が渡される問題の解答コードを書く方法として、問題ページに直接書く方法や、paiza.io等のオンライン実行環境を使う方法がありますが、今回はローカルに環境を構築してみます。

※この方法はpaizaの他に、AtCoder等の競技プログラミングの問題を解く場合も使えます。

ローカルでコードを書くメリット

  • お好みのフォーマッター・リンターを導入できる
  • 使い慣れたキーボードショートカットが使える

その他いろいろカスタマイズでき、自由度が高いというメリットがあります。

環境

  • Windows 10
  • Visual Studio Code
  • node v16.13.2

環境構築の手順

作業用のフォルダを作成し、その中に以下の名前のファイルを作成します。

  • case1.txt
  • case2.txt
  • case3.txt
  • case4.txt
  • case5.txt
  • main.js
  • main.template.js
  • package.json

case1.txt ~ case5.txt

動作確認として渡す標準入力を、これらのファイルにそれぞれ貼り付けます。

(例)case1.txt
3 5
.#...
...#.
#..#.

main.js

解答のコードを書くファイルです。書き終わったらコピーして提出しましょう。

main.template.js

新しくコードを書き始める時のテンプレート用のファイルです。
標準入力は配列argsに1行ずつ入ります。

main.template.js
process.stdin.resume().setEncoding("utf8");
const args = [];
require("readline")
  .createInterface({ input: process.stdin })
  .on("line", (input) => args.push(input))
  .on("close", main);

function main() {
  // ここからコードを書き始める
}

package.json

標準入力を渡してプログラムを実行するためのコマンドのショートカットを記述します。

package.json
{
    "scripts": {
        "1": "node main < case1.txt",
        "2": "node main < case2.txt",
        "3": "node main < case3.txt",
        "4": "node main < case4.txt",
        "5": "node main < case5.txt",
        "new": "cp main.template.js main.js && rm case1.txt && touch case1.txt && rm case2.txt && touch case2.txt && rm case3.txt && touch case3.txt && rm case4.txt && touch case4.txt && rm case5.txt && touch case5.txt"
    }
}

画面レイアウトの例

ウィンドウの右側に、標準入力ファイルのタブを配置させるのがおすすめです。

VSCode 2021-12-11 215030.png

ターミナルで使うコマンド

初期化コマンド

main.template.jsの中身をmain.jsにコピーし、case1.txt ~ case5.txtの中身を空にします。

npm run new

実行コマンド

case1.txt ~ case5.txtのそれぞれの標準入力を渡してプログラムを実行できます。

npm run 1
npm run 2
npm run 3
npm run 4
npm run 5
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3