[PYTHON] Convenient! Introducing AtCoder Tools, which comes in handy when programming competitives with AtCoder

Introduction

How do you program when solving AtCoder problems? Basically, I think that you will write and submit the program with the editor of the local environment.

When you are solving a problem, these requests come up.

――It's all done! I want to test with the input value listed in the question sentence! ――The first input value was correct, but what about the second and third? It's annoying to check one by one ... --The program is complete, so let's submit it! I want to submit it as it is from the CLI!

The CLI tool that meets these demands is AtCoder Tools. This article will introduce you from installation to basic usage.

I want to use it quickly because the long explanation is good?

_Done. I will explain only the minimum usage. _

#Installation
$ pip install atcoder-tools

#Batch download of input / output values and generate code from template
##abs is the contest ID
## --Specify the download path in workspace
## --Specify the language of the code automatically generated by lang
## --without-Download without logging in with login
$ atcoder-tools gen abs --workspace=path/to/atcoder-workspace/ --lang=python --without-login

#Run tests together for multiple input values
$ cd path/to/program-location
$ atcoder-tools test

#Submission of the program
$ atcoder-tools submit

environment

environment version Installation source
AtCoder Tools 1.1.7.1 pipenv(pip)
pipenv 2020.8.13 pip
pip 20.1.1 (python 3.8) pyenv
Python 3.8.5 pyenv
pyenv 1.2.20-3-g58c776a1 anyenv
anyenv 1.1.1 Homebrew
Homebrew 2.4.9 n/a
macOS 10.15.6 n/a

Installation method

AtCoder Tools is a tool written in Python. It can be installed using pip. I'd like to install from Homebrew, but since there is no formula available, for now I can only install using pip.

$ pip install atcoder-tools

Or

Pipfile


[[source]]
url = 'https://pypi.python.org/simple'
verify_ssl = true
name = 'pypi'

[requires]
python_version = '3.8'

[packages]
atcoder-tools = '*'
$ pip install pipenv #If pipenv is not installed
$ pipenv install --system

: warning: Attention

AtCoder Tools seems to be guaranteed to work only with Python 3.5 and above. Therefore, you need to use pip 3 series (Python 3 series) when installing.

You need to replace pip with pip3, pip3.8, etc. as needed. Execute the following command to confirm that it is Python 3 series in advance.

#If the result is Python 2 series, use pip3 etc.
$ pip --version
pip 20.1.1 from /Users/noraworld/.anyenv/envs/pyenv/versions/3.8.5/lib/python3.8/site-packages/pip (python 3.8)

You may also need to restart the shell after installation.

$ exec -l $SHELL

When you want to download input / output values at once

You can copy the input / output values listed in the problem statement and paste them into a file ... but it is troublesome to do it every time you solve the problem.

With AtCoder Tools, you can download all the input / output values of all the questions in the contest at once with the following command.

For example, to download all the input / output values of all 11 questions of AtCoder Beginners Selection, which is said to be the first thing to do after registering with AtCoder: Execute the command of.

$ atcoder-tools gen abs --workspace=path/to/atcoder-workspace/ --lang=python --without-login

Then it will be downloaded like this under path / to / atcoder-workspace /.

Directory tree (click to expand)
.
├── ABC049C
│  ├── in_1.txt
│  ├── in_2.txt
│  ├── in_3.txt
│  ├── main.py
│  ├── metadata.json
│  ├── out_1.txt
│  ├── out_2.txt
│  └── out_3.txt
├── ABC081A
│  ├── in_1.txt
│  ├── in_2.txt
│  ├── main.py
│  ├── metadata.json
│  ├── out_1.txt
│  └── out_2.txt
├── ABC081B
│  ├── in_1.txt
│  ├── in_2.txt
│  ├── in_3.txt
│  ├── main.py
│  ├── metadata.json
│  ├── out_1.txt
│  ├── out_2.txt
│  └── out_3.txt
├── ABC083B
│  ├── in_1.txt
│  ├── in_2.txt
│  ├── in_3.txt
│  ├── main.py
│  ├── metadata.json
│  ├── out_1.txt
│  ├── out_2.txt
│  └── out_3.txt
├── ABC085B
│  ├── in_1.txt
│  ├── in_2.txt
│  ├── in_3.txt
│  ├── main.py
│  ├── metadata.json
│  ├── out_1.txt
│  ├── out_2.txt
│  └── out_3.txt
├── ABC085C
│  ├── in_1.txt
│  ├── in_2.txt
│  ├── in_3.txt
│  ├── in_4.txt
│  ├── main.py
│  ├── metadata.json
│  ├── out_1.txt
│  ├── out_2.txt
│  ├── out_3.txt
│  └── out_4.txt
├── ABC086A
│  ├── in_1.txt
│  ├── in_2.txt
│  ├── main.py
│  ├── metadata.json
│  ├── out_1.txt
│  └── out_2.txt
├── ABC086C
│  ├── in_1.txt
│  ├── in_2.txt
│  ├── in_3.txt
│  ├── main.py
│  ├── metadata.json
│  ├── out_1.txt
│  ├── out_2.txt
│  └── out_3.txt
├── ABC087B
│  ├── in_1.txt
│  ├── in_2.txt
│  ├── in_3.txt
│  ├── main.py
│  ├── metadata.json
│  ├── out_1.txt
│  ├── out_2.txt
│  └── out_3.txt
├── ABC088B
│  ├── in_1.txt
│  ├── in_2.txt
│  ├── in_3.txt
│  ├── main.py
│  ├── metadata.json
│  ├── out_1.txt
│  ├── out_2.txt
│  └── out_3.txt
└── PracticeA
   ├── in_1.txt
   ├── in_2.txt
   ├── main.py
   ├── metadata.json
   ├── out_1.txt
   └── out_2.txt

The file ʻin_ * .txt is the input value, and ʻout_ * .txt is the output value. main.py is the code automatically generated from the template.

Explanation about arguments

Use the gen subcommand.

The following ʻabsis the contest name (contest ID). It is the part of<CONTEST_ID>ofhttps://atcoder.jp/contests/ <CONTEST_ID>`.

Specify the download path with the --workspace option.

Specify the programming language with --lang. The code (which seems to be parsing the input format etc.) is automatically generated from the template of the language specified here. Please see here for supported languages. It seems that my favorite Ruby is not yet supported. Sorry!

If you add --without-login, you can download without logging in. If you do not enter it, you will be asked for your ID and password. If you can download it without logging in, it will be easier to add it.

In summary, it looks like this.

argument Description Default value if omitted
gen Subcommand name (Cannot be omitted)
abs Contest ID (Cannot be omitted)
--workspace Path to download ~/atcoder-workspace
--lang Language of automatically generated template code cpp (C++)
--without-login Use without logging in nil (You are required to log in)

There is an issue "gen command --without-login should be the default # 99", so maybe a future version In, --without-login may be the default.

It's annoying to specify options every time ...

It can be annoying to specify options such as --workspace and --lang every time.

In AtCoder Tools, you can write settings in ~ / .atcodertools.toml and omit options.

toml:~/.atcodertools.toml


[codestyle]
workspace_dir='~/Workspace/competitive_programming/atcoder/'
lang='python'

[etc]
download_without_login=true

Set the path specified by --workspace to workspace_dir of [codestyle] and the language specified by --lang to lang of [codestyle].

I was wondering if I could omit --without-login by setting download_without_login = true to [etc], but I couldn't (I was asked to log in). It may be used incorrectly or it may be a bug.

When you want to test with multiple I / O values at once

By the way, the input / output values are all set. You want to compare the output value given to the program with the correct output value.

If you do it honestly without using AtCoder Tools, this will happen.

$ cat in_1.txt | python main.py
#The output result is displayed

$ cat out_1.txt
#Compare if it is the same as the previous output result

$ cat in_2.txt | python main.py
#Do the same for the second test case

$ cat out_2.txt
#Same result checked

$ cat in_3.txt | python main.py
#Third one

$ cat out_3.txt
#check

# ...The same applies below

...... It's annoying, isn't it?

With AtCoder Tools, you can check all at once with a single command.

$ cd path/to/program-location #Move to the directory containing the program and I / O files
$ atcoder-tools test
# in_1.txt ... PASSED 163 ms
# in_2.txt ... PASSED 136 ms
# in_3.txt ... PASSED 130 ms
Passed all test cases!!!

It's very convenient!

Any non-supported language is OK as long as it is an executable file!

The program tested with this command is a file with execute permissions.

In other words, if you set shebang in your program and give it execute permission, you can test programs in languages that AtCoder Tools does not support (for example, Ruby)!

main.rb


#!/usr/bin/env ruby

#↑ Add shebang on the first line
#Write a program
#Give execute permission
$ chmod +x main.rb

Whether or not the language is supported is just the code generated from the template, and it seems that there are no particular restrictions on the language in which the test is executed.

About options

If you want to run the test from a directory other than the current directory, you can do so by adding the --dir option.

Also, the program (file with execute permission) is automatically detected, but if there are multiple files, one of them is automatically selected. If you have multiple programs and want to specify a specific program, I think you should add the --exec option, but in my environment I got a FileNotFoundError and it didn't work.

When you want to submit a program from the CLI

I haven't used this yet, so I'm writing it by intuition. I'm sorry if I made a mistake: bow:

Well, the test passed, and finally submitted! You can copy and submit the program, but AtCoder Tools has a program submission feature. It makes it easy to submit programs from the CLI.

$ atcoder-tools submit

Only this.

You probably need to log in (or for sure with the submit command), so enter your AtCoder ID and password when prompted for an ID and password.

I want to know more details!

See the README. It is written in Japanese, so even those who are not good at English can do it.

at the end

I'm still atCoder for 2 days [^ 1], but I quickly realized that it would be more convenient to have this tool, so I installed it in haste.

[^ 1]: Because it is "2 days", it is a "unbound person" ^ 2.

Since I am solving the problem with Ruby so far, there are problems such as code generation is not compatible with Ruby, options and settings are not available in some places, but these will be gradually improved.

(By the way, I thought I'd send a PR for Ruby, but I was frustrated because I'm new to Python and have almost no experience contributing to libraries ... w).

Recommended Posts

Convenient! Introducing AtCoder Tools, which comes in handy when programming competitives with AtCoder
Things to keep in mind when using Python with AtCoder