1
0

はじめに

ABC参加2回目の灰色コーダーです。VSCodeとブラウザを行き来するのが面倒くさいので、便利なパッケージを使って環境構築してみよう録です。
pythonもあまりよく知りません。

image.png
これはバイソン

備忘録としてQiitaを作成しました。

筆者は初学者のため、間違いや冗長な部分があると思われますのでご了承ください。

参考にさせていただいた記事様

こちらの記事を参考に環境構築させていただきました。

上の記事で紹介されているのに習います。

  • online-judge-tools
  • atcoder-cli

を使います。

環境構築

以下のMacOSで環境を構築していきます。

% sw_vers
ProductName:            macOS
ProductVersion:         14.5
BuildVersion:           23F79

また、brewnpm及びNode.jsはインストール済みであるとします。

構築方法は上記atcoder-cliのチュートリアルを上からやっていきます。実際に環境構築したい方は、上記のチュートリアルを参考にしてください。


早速つまづきます。

pip3online-judge-toolsをインストールできません。python3brewでインストール済みです。

pip3 install online-judge-tools
% pip3 install online-judge-tools
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:
    
    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz
    
    If you wish to install a Python application that isn't in Homebrew,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. You can install pipx with
    
    brew install pipx
    
    You may restore the old behavior of pip by passing
    the '--break-system-packages' flag to pip, or by adding
    'break-system-packages = true' to your pip.conf file. The latter
    will permanently disable this error.
    
    If you disable this error, we STRONGLY recommend that you additionally
    pass the '--user' flag to pip, or set 'user = true' in your pip.conf
    file. Failure to do this can result in a broken Homebrew installation.
    
    Read more about this behavior here: <https://peps.python.org/pep-0668/>

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

[notice] A new release of pip is available: 24.0 -> 24.1
[notice] To update, run: python3.12 -m pip install --upgrade pip

AnacondaさんのTerminalに投げたらインストールできました。

image.png

なんか、Dockerとかで環境構築したらいちいちこんな回りくどいことしなくてもいいのかもしれないと思っています。

それでは、online-judge-toolsが存在するかチェックしてみましょう。

% acc check-oj
online-judge-tools is not available. 

できてませんでした。
pipは何故かうまくいきました。

zsh
% pip install online-judge-tools
Collecting online-judge-tools
  Using cached online_judge_tools-11.5.1-py3-none-any.whl.metadata (9.1 kB)
Collecting online-judge-api-client<11,>=10.9.0 (from online-judge-tools)
  Using cached online_judge_api_client-10.10.1-py3-none-any.whl.metadata (16 kB)
...

もう一度online-judge-toolsがあるか確かめてみましょう。
anaconda3/...の中に見つかったとあります。

zsh: acc check-oj
% acc check-oj
online-judge-tools is available. found at:
/opt/anaconda3/envs/py_env/bin/oj

実際に動かしてみる

実際に環境を構築できたら、次のonline-judge-toolsの日本語リファレンスを参照しながらSubmitやテストケースを実行するコマンドを叩いてみます。

まず、accojでログインする必要があります。

acc loginでログイン
% acc login
? username: 
? password: [hidden]
OK
% acc session
check login status...
OK
oj login ...atcoder.jp でログイン
% oj login https://beta.atcoder.jp/
[INFO] GET: https://pypi.org/pypi/online-judge-tools/json
[INFO] 200 OK
...

チュートリアルに習って、ABCの101で試してみます。
なんと!テストケースを./tests/配下に作成してセーブしています。

% acc new abc101
abc101/contest.acc.json created.
create project of AtCoder Beginner Contest 101
? select tasks A Eating Symbols Easy
[INFO] online-judge-tools 11.5.1 (+ online-judge-api-client 10.10.1)
[INFO] load cookie from: /Users/USER_NAME/Library/Application Support/online-judge-tools/cookie.jar
[NETWORK] GET: https://atcoder.jp/contests/abc101/tasks/abc101_a
[NETWORK] 200 OK
[INFO] save cookie to: /Users/USER_NAME/Library/Application Support/online-judge-tools/cookie.jar

[INFO] sample 0
[INFO] input: sample-1
+-++

[SUCCESS] saved to: tests/sample-1.in
[INFO] output: sample-1
2

[SUCCESS] saved to: tests/sample-1.out

[INFO] sample 1
[INFO] input: sample-2
-+--

[SUCCESS] saved to: tests/sample-2.in
[INFO] output: sample-2
-2

[SUCCESS] saved to: tests/sample-2.out

[INFO] sample 2
[INFO] input: sample-3
----

[SUCCESS] saved to: tests/sample-3.in
[INFO] output: sample-3
-4

[SUCCESS] saved to: tests/sample-3.out

また、abc101ディレクトリにa(ディレクトリ)とcontest.acc.jsonが格納されていることがわかります。

zsh
% cd abc101 
% ls
a
contest.acc.json

aはA問題です。早速aディレクトリで提出するファイルmain.c (main.py, main.cppなど)を作成してみます。

% cd a/
% touch main.c

話を簡単にするために、提出用ファイルは正しく作成できたものとします。

テストケースを実行

作成したソースコード(main.py, huga.cpp, hoge.c)など実行ファイルとしてa.out
以下のコマンドでテストケースを実行します。

zsh
% oj t -d ./tests/

ソースコードがC,C++以外の場合はドキュメントを参照してください。

実行ファイルとしてa.outを作成しましょう。
これがないと次のような[ERROR]が起こります。

また、gnu-timeをインストールしてねと言われます。

[INFO] online-judge-tools 11.5.1 (+ online-judge-api-client 10.10.1)
[INFO] 3 cases found
[WARNING] GNU time is not available: gtime
[HINT] You can install GNU time with: $ brew install gnu-time

[INFO] sample-1
[ERROR] No such file or directory: ['./a.out']
gnu-timeのinstall
% brew install gnu-time
=> Downloading...
% oj t -d ./tests/     
[INFO] online-judge-tools 11.5.1 (+ online-judge-api-client 10.10.1)
[INFO] 3 cases found

[INFO] sample-1
[INFO] time: 0.007112 sec
[SUCCESS] AC

[INFO] sample-2
[INFO] time: 0.006529 sec
[SUCCESS] AC

[INFO] sample-3
[INFO] time: 0.005459 sec
[SUCCESS] AC

[INFO] slowest: 0.007112 sec  (for sample-1)
[INFO] max memory: 0.976000 MB  (for sample-1)
[SUCCESS] test success: 3 cases

Submit

accコマンドでsubmitします。

% acc submit main.c 
submit to: https://atcoder.jp/contests/abc101/tasks/abc101_a

ブラウザを見てみても
実際に提出されていることが確認できます。

image.png

まとめ

online-judge-toolsatcoder-cliの環境構築し、実際にテストケースの実行とソースファイルの提出を行いました。

繰り返しになりますが、筆者は初学者のため、間違いや冗長な部分があると思われますのでご了承ください。

おさらい

テストケース実行→提出までの過程を簡単に振り返ってみましょう。

  1. acc loginでログイン
  2. oj login https://beta.atcoder.jp/でもログイン
  3. acc new abcXXX でディレクトリ作成とテストケースセーブ
  4. cd abcXXX/aなどで、ABCXXXのA問題などのディレクトリに移動
  5. 4.にて移動したディレクトリ内で提出用ファイル(hoge.cpp)作成
  6. 実行ファイル(a.out)を作成
  7. oj t -d ./tests/でテストケース実行
  8. acc submit hoge.cppで提出
1
0
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
1
0