[PYTHON] Get, test, and submit test cases on the command line in the AtCoder contest

Introduction

It is a procedure to acquire, test, and submit a test case on the terminal (Mac) using ʻatcoder-cli, ʻonline-judge-tools.

When I started AtCoder, I used the code test on the contest page without any special environment maintenance, but by improving the environment as described in this article, everything will be completed on Visual Studio Code + iTerm2. The speed of submission has increased.

Advance preparation

Installation of necessary tools

$ pip3 install online-judge-tools
$ npm install -g atcoder-cli
$ pip3 install selenium

Log in to AtCoder with the tool

$ acc login
$ oj login https://atcoder.jp/

Setting

Make sure that all problem directories are created. For example, for AtCoder Begginer Contest, let the acc new abcXXX command create problems a ~ f

$ acc config default-task-choice all

Show the location of the config directory

$ acc config-dir

Set the template. Create a folder with the template name in the config directory, and create the template source code (example: main.py) and the template configuration file (template.json) in it.

#Go to config directory
$ cd /Users/xxxxxxxxxx/Library/Preferences/atcoder-cli-nodejs
$ mkdir py
$ cd py
$ touch main.py template.json

When the problem directory is created, main.py is created, and to submit it to main.py, write as follows.

$ cat template.json 
{
  "task":{
    "program": ["main.py"],
    "submit": "main.py"
  }
}

Edit the default settings when main.py was created. #! / usr / bin / env python3 is required. In my case, I write additional common input patterns by default.

$ cat main.py 
#!/usr/bin/env python3
S = input()
N = int(input()) 
S = input().split()
A, B, C = input().split()
L = list(map(int, input().split()))
H, N = map(int, input().split())

Set the default template to py (files set in the py directory apply)

$ acc config default-template py

Check template

$ acc templates
search template directories in /Users/xxxxxxxxxx/Library/Preferences/atcoder-cli-nodejs
NAME  SUBMIT-PROGRAM
py    main.py

Directory creation

The example below is for creating a directory for AtCoder Bergginer Contest 154. A file of main.py and test input and output is created.

$ acc new abc154
$ tree
.
├── a
│   ├── main.py
│   └── tests
│       ├── sample-1.in
│       ├── sample-1.out
│       ├── sample-2.in
│       └── sample-2.out
├── b
│   ├── main.py
│   └── tests
│       ├── sample-1.in
│       ├── sample-1.out
│       ├── sample-2.in
│       ├── sample-2.out
│       ├── sample-3.in
│       └── sample-3.out
├── c
│   ├── main.py
│   └── tests
│       ├── sample-1.in
│       ├── sample-1.out
│       ├── sample-2.in
│       ├── sample-2.out
│       ├── sample-3.in
│       └── sample-3.out
├── contest.acc.json
├── d
│   ├── main.py
│   └── tests
│       ├── sample-1.in
│       ├── sample-1.out
│       ├── sample-2.in
│       ├── sample-2.out
│       ├── sample-3.in
│       └── sample-3.out
├── e
│   ├── main.py
│   └── tests
│       ├── sample-1.in
│       ├── sample-1.out
│       ├── sample-2.in
│       ├── sample-2.out
│       ├── sample-3.in
│       ├── sample-3.out
│       ├── sample-4.in
│       └── sample-4.out
└── f
    ├── main.py
    └── tests
        ├── sample-1.in
        ├── sample-1.out
        ├── sample-2.in
        └── sample-2.out

12 directories, 41 files

test

Once the program is complete, test it using ʻonline-judge-tools` (atcoder-cli has no test commands)

$ oj t -c "python3 main.py" -d ./tests/

In my case, I put alias so that I can test with only the test command in the directory of the target problem.

$ vim ~/.bash_profile

#Add the following
alias test='oj t -c "python3 main.py" -d ./tests/'

$ source ~/.bash_profile

Run the test command in the directory of interest

$ pwd
/xxxxx/atcoder/problem/abc151/a
$ test

If you want to test in a test case other than the problem statement, such as during debugging, execute the program normally and enter the test case. In the case of the following programs

n = int(input()) 
print(n)

Check if the expected result is returned by getting the output by executing and inputting as follows.

$ python main.py
1 #input
1 #output

Submission

By executing the following command in the directory in question, it will automatically jump to the AtCoder page and submit it.

$ acc s

reference

Recommended Posts

Get, test, and submit test cases on the command line in the AtCoder contest
Keep getting RSS on the command line
You search commandlinefu on the command line
Easy way to enter and execute AtCoder test cases in Jupyter Notebook
Get options in Python from both JSON files and command line arguments
Quickly display the QR code on the command line
Arduino development on the command line: vim + platformio
Syntax highlighting on the command line using Pygments
Convert XLSX to CSV on the command line
Operate Route53 on the command line using AWS-CLI.
I want to get the file name, line number, and function name in Python 3.4
Think about the selective interface on the command line
Get the latest schedule from Google Calendar and notify it on LINE every morning
Let's get notified of the weather in your favorite area from yahoo weather on LINE!
Execute the command on the web server and display the result
Let's get notified of the weather in your favorite area from yahoo weather on LINE! ~ PART2 ~
Get and create nodes added and updated in the new version
Search for large files on Linux from the command line
Get the MIME type in Python and determine the file format
[Jinja2] Changes in line breaks depending on the hyphen position
Search for variables in pandas.DataFrame and get the corresponding row.
I tried programming the chi-square test in Python and Java.
How to get all the keys and values in the dictionary
Get the current date and time in Python, considering the time difference
How to log in to AtCoder with Python and submit automatically
How to copy and paste command line content without mouse in bash on Linux or mac
python Note: Determine if command line arguments are in the list
Create AtCoder Contest appointments on Google Calendar with Python and GAS
How to get a string from a command line argument in python
[EC2] How to install and download chromedriver from the command line
Create custom Django commands and run them from the command line
Python standard module that can be used on the command line
Goodbye to the command prompt in WSL, VSCode and Windows Terminal
Get the title and delivery date of Yahoo! News in Python
Format the Git log and get the committed file name in csv format
Get the number of readers of a treatise on Mendeley in Python
Install Chrome on the command line on Sakura VPS (Ubuntu) and launch Chrome with python from virtual display and selenium
After installing Anaconda3, I get the error "zsh: no such file or directory:/opt/anaconda3/bin/conda" in the command line preferences.
Introducing youtube-dl, a video download tool that runs on the command line, and its zsh completion function.