AtCoder: Python: Automate sample testing as much as possible.

It is troublesome to copy, paste, and execute the contest input example with AtCoder to check if it matches, so I want to automate it as much as possible. But I want to make a corner case by myself and test it, so I want to be able to execute the case I made.

If you have python installed on windows, you can do it by following the steps below.

After completing all the steps first, I will explain how to test in the actual contest. ① Execute the sample download program and enter the contest name (such as "abc123") (2) Download input example and output example ③ Write a program ④ Execute and check if the output example matches the output

Below is a brief description of what the program is doing. ① Receive the entered contest name (2) Access the URL from the contest name with python and pull out the input example and output example from html. ③ Output each to a txt file ④ Execute the program with the saved txt file as an argument (use windows bat) (5) Display execution result output and output example side by side

The introduction method and usage are described below.

【step 1】 At the command prompt pip install requests pip install BeautifulSoup To install the library used to get the URL.

[Procedure 2] Create folders called "Samples" and "Run". (The place can be anywhere)

[Procedure 3] Create a file called GetSample.py in the "Samples" folder and paste the following

GetSample.py


import requests,os,glob
from bs4 import BeautifulSoup

def outtxt(pro):
    res=requests.get("https://atcoder.jp/contests/"+No+"/tasks/"+No+"_"+pro)
    soup=BeautifulSoup(res.text, 'html.parser')

    source=soup.find_all("pre")
    source=[str(source) for source in source]

    sample=[]
    for i in range(1,len(source)):
        if "<var>" in source[i]:
            break
        else:
            tmp=source[i]
            tmp=tmp.replace("<pre>","")
            tmp=tmp.replace("</pre>","")
            tmp=tmp.replace("\r\n","\n")
            sample.append(tmp)

    for i in range(0,len(sample),2):
        with open("Sample_Input_"+pro.upper()+"_"+str(i//2+1)+".txt", mode='w') as f:
            f.write(sample[i])

    for i in range(1,len(sample),2):
        with open("Sample_Output_"+pro.upper()+"_"+str(i//2+1)+".txt", mode='w') as f:
            f.write(sample[i])
    
No=input()

for file in glob.glob('*.txt'):
    os.remove(file)

outtxt("a")
outtxt("b")
outtxt("c")
outtxt("d")
outtxt("e")
outtxt("f")

[Procedure 4] Double-click GetSample.py to execute it. → Enter the contest name such as "abc123" because it will stop waiting for input. → You can create txt files of input examples and output examples in the "Samples" folder.

[Procedure 5] Create an empty txt file called "HandInput.txt" in the Run folder.

[Procedure 6] Create "Ans.py" in the Run folder and paste the following

Ans.py


import io,sys,os
if len(sys.argv)>1:
    path=sys.argv[1]
else:
    os.system('cls')
    path="HandInput.txt"
try:
    with open(path) as f:
        INPUT=f.read() 
    sys.stdin = io.StringIO(INPUT)
except:
    print("The specified file could not be found.")
    exit()
# --------------------------------------------------------
#The submission code is listed below

[Procedure 7] Create "SampleTest_A.bat" in the "Run" folder and paste the following (For "samples folder path", write the path of the location where the samples folder was created (like C: \ Windows \ ... \ Samples))

SampleText_A.bat


echo off
echo [Case1]
Ans.py "path of samples folder"\Sample_Input_A_1.txt
type "samples folder path"\Sample_Output_A_1.txt
echo [Case2]
Ans.py "path of samples folder"\Sample_Input_A_2.txt
type "samples folder path"\Sample_Output_A_2.txt
echo [Case3]
Ans.py "path of samples folder"\Sample_Input_A_3.txt
type "samples folder path"\Sample_Output_A_3.txt
echo [Case4]
Ans.py "path of samples folder"\Sample_Input_A_4.txt
type "samples folder path"\Sample_Output_A_4.txt

Create sampleTest_B.bat, sampleTest_C.bat, sampleTest_D.bat, sampleTest_E.bat, sampleTest_F.bat in the same way as above (rewrite all the parts written as A in the bat to B, C, D ... )

[Procedure 8] Create SampleTest.py in the "Run" folder and paste the following. As a caveat, even if you write the folder path directly in python, an error will occur, so double the \ in the file path for the "samples folder path" part. (Example C \ windows \ ・ ・ ・ \ Samples → C \\ windows \\ ・ ・ ・ \\ Samples)

SampleTest.py


import os,sys
os.system('cls')
pro="A"
os.system(""Samples folder path"\\SampleTest_"+pro+".bat")

This completes the preparation.

【how to use】 First, write the code in the "Enter the submission code here" section of Ans.py. For A problem, set "pro =" A "" in SamplesTest.py, and for B problem, set "pro =" B "" and execute.

SamplesTest.py,Output example


[Case1]
1
1      
[Case2]
0
0      
[Case3]
10
10     
[Case4]
The specified file could not be found.
The specified file could not be found.

If you have a sample output example and your own answer, it will look like this. Above is my answer. Below is an example of sample output. If they are compared, submit them. If you want to try your own sample, write the input in HandInput.txt and execute Ans.py as it is.

Summary,

The contents of the "Run" folder are as follows. Ans.py GetSample.py SampleTest.py HandInput.txt

The contents of the "Samples" folder are as follows. GetSample.py SampleTest_A.bat SampleTest_B.bat SampleTest_C.bat SampleTest_D.bat SampleTest_E.bat SampleTest_F.bat "Other sample input (Sample_Input_A_1.txt, etc.) and output (Sample_Output_A_1.txt) if GetSample.py is executed"

Access the problem page of atcoder with GetSample.py → Extract input example and output example from html file and write to text file If there is an argument when executing Ans.py, it will be accepted as input, otherwise HandInput.txt will be accepted as input. SampleTest_A.bat etc. is a bat that is executed by passing the text file output by GetSample.py to Ans.py as an argument. SampleTest.py is a program that just executes SampleTest_A.bat etc. (You can test by hitting bat directly without it, but I just made it because I wanted to do it all on VScode)

Recommended Posts

AtCoder: Python: Automate sample testing as much as possible.
Automate AtCoder submission (Python)
Automate python testing with CircleCI
AtCoder: Python: Daddy the sample test.
Automate AtCoder directory creation, sample case testing, and submission. atcoder-cli and online-judge-tools
atCoder 173 Python
Find prime numbers in Python as short as possible
Use get as much as possible for dictionary-type references
AtCoder ABC 174 Python
Python closure sample
AtCoder ABC187 Python
AtCoder ABC188 Python
AtCoder ABC 175 Python
Automate UI testing with Selenium API | Crawling websites with python
How to run Python on Windows without polluting the environment as much as possible (Scoop edition)