Competitive Pro with Python and VSCode-Simplification of standard input and automation of sample case judgment-

I recently started a competitive pro at AtCoder: beginner: It's nice to start, but it's a debugging hell over and over again because it doesn't solve quickly: sob: Every time I copy a test case to the terminal ... Let's stop doing that: no_entry_sign:

This article windows10 1909 VSCode 1.45.1 Python 3.8.2 It is the content confirmed in.

1. Use sys.stdin for debugging

First is the simplification of standard input.

When searching by conditions such as "VS Code python standard input", teratail had such a question and answer How to receive standard input with VS Code (Windows10 / Python) I don't know if it's famous in the professional competition, but that's exactly what I was looking for.

import io
import sys

_INPUT = """\
2
1 2 3
aaa
"""
sys.stdin = io.StringIO(_INPUT)

print(int(input()))
print(list(map(int, input().split())))
print(list(input()))

#output
2
[1, 2, 3]
['a', 'a', 'a']

According to the Python documentation, sys.stdin is all interactive (including calling ʻinput ()`) Used for input, io.StringIO is basically a class that reads and writes strings like a file. It seems like.

Templates

test_inut.py


import io
import sys

# input here
_INPUT = """\

"""
sys.stdin = io.StringIO(_INPUT)

# your code here

</ div> Enter a sample case, a test case you think of yourself, and the code you want to debug. </ details> Above all, it's nice to see the test case at a glance, unlike writing it in a separate file (input.txt, etc.). Now you don't have to worry about the copype storm: clap:

2. Let's use online-judge-tools

There is a tool to automate the judgment of sample cases! !! that is online-judge-tools is.

online-judge-tools is a great tool for scraping sample cases from the contest site and automating the decision.

Linux or Mac OS is recommended as the OS, but it also works on Windows.

As it says, it works fine in my environment.

How to use

For usage, see the GitHub page, as well as Building a competitive professional environment with Visual Studio Code (practice) Automatic sample test case & manual input value test execution with VS Code with AtCoder I was allowed to refer to.

Folder structure
AtCoder\
├── .venv\
│
├── .vscode\
│   └── tasks.json
│
├── sctipt\
│   └── cptest.ps1
│
├── src\
│   └── abc000_0.py
│
└── test\
    └── abc000_0\
        ├── sample-1.in
        └── sample-1.out

Replace the .venv folder with the name you used when you built the virtual environment. It does not have to be. The .vscode folder is created automatically when you generate tasks.json. The script and src folders are not absolutely necessary. The test folder is created automatically when you run cptest.ps1.

1. Installation
> pip install online-judge-tools

Easy to install with pip.

2. Login

Enter your username and password.

> oj login -u username-p password"https://atcoder.jp/"

To check if you can log in

> oj login --check "https://atcoder.jp/"
[*] You have already signed in.

If it returns, it's OK.

  1. cptest.ps1 A script that takes sample cases from the contest site and tests them.

The following is the script that I referred to as .ps1 and made changes.

cptest.ps1


. ".venv/Scripts/Activate.ps1"

$problem_name = $Args[0]
$problem_name_list = ($problem_name -split "_")
$base_url = ($problem_name.Replace("_", "-")).Substring(0, ($problem_name.Length) - 2)

if (! (Test-Path $test_dir)) {
    oj dl -d test/$problem_name/ https://atcoder.jp/contests/$base_url/tasks/$problem_name
}

oj test -c "python src/$problem_name.py" -d test/$problem_name/

I have installed online-judge-tools in the virtual environment inside the AtCoder folder, so the first line loads .venv / Scripts / Activate.ps1 and activates the virtual environment. Not required if you are not in a virtual environment.

Also, URLs such as 2nd National Unified Programming King Final Qualifying A --Sum of Two Integers https://atcoder.jp/contests/nikkei2019-2-qual/tasks/nikkei2019_2_qual_a Since a hyphen is used in $ base_url and an underscore is used in $ problem_name, a process to change _ to - is added. (It has changed.)

  1. tasks.json VS Code's ** Tasks ** are features that automate a variety of tasks and can be performed with Ctrl + Shift + B. So let's make it possible to run cptest.ps1.

Open the command palette with Ctrl + Shift + P and type task to see many commands. From that, select "Task Configuration"-> "Generate tasks.json from template"-> "Others" to generate tasks.json.

tasks.json


{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test_atcorder_sample",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "type": "shell",
            "command": "${workspaceFolder}/script/cptest.ps1",
            "args": [
                "${fileBasenameNoExtension}"
            ]
        }
    ]
}

Now, if the file name is ʻabc060_b.py, press Ctrl + Shift + B`

> cptest.ps1 abc060_b

You have executed.

That's all for how to use it. The tool can also be submitted and stress tested. How much would you like to be able to automate the testing of sample cases: joy: I haven't mastered it yet, so I'll do my best from now on.

at the end

It's really easier to debug. I hope this article can help other professional beginners as much as possible. Have a good competition professional life: wave:

Recommended Posts