Python script to get a list of input examples for the AtCoder contest

Source code

Click here for Gist https://gist.github.com/029969994cb444bef7ed

# -*- coding: utf-8 -*-
from urllib.request import urlopen
from bs4 import BeautifulSoup
import sys
import os
import re
'''
If you pass the URL of the contest top page, it will be under the execution directory
Creating a directory for each problem,Output the problem page input example to a file
'''

INPUT_FILE_NAME = "input.txt"
DELIMITER = "---"


def main():
    # http://arc001.contest.atcoder.jp/
    try:
        url = sys.argv[1]
    except IndexError:
        print("input contest url:")
        url = input()

    m = re.search('(https?://)(?P<name>.*)\.contest?', url)
    contest_name = m.group('name')
    if not os.path.exists(contest_name):
        os.mkdir(contest_name)
    page = urlopen(url + "assignments")
    soup = BeautifulSoup(page)
    table = soup.find('table', attrs={'class': 'table-wb'}).find('tbody')
    for tr in table.find_all('tr'):
        (sharptd, qlinktd) = tr.find_all('td', limit=2)
        index = sharptd.text
        q_path = contest_name + "/" + index
        if not os.path.exists(q_path):
            os.mkdir(q_path)
        url_tail = qlinktd.find('a').get('href')
        f_path = q_path + "/" + INPUT_FILE_NAME
        if not os.path.exists(f_path):
            f = open(f_path, "w")
            f.write(soup_prets(url + url_tail[1:]))
            f.close()
            print("generate input > " + f_path)
        else:
            print("already exists: f_path")


def soup_prets(url):
    '''
fetch input text
    '''

#    print(url)
    page = urlopen(url)
    soup = BeautifulSoup(page)
    inputs = []
    for pretag in soup.find_all("pre", attrs={"class": "prettyprint"})[::2]:
        inputs.append(pretag.string.strip())
    return ("\n" + DELIMITER + "\n").join(inputs)

if __name__ == '__main__':
    main()

Preparation

Requires Python3, BeautifulSoup4

pip install beautifulsoup4 

Execution example

Try in the contest at http://arc001.contest.atcoder.jp/

> ls
start.py

Run script

> ./start.py http://arc001.contest.atcoder.jp/
generate input > arc001/A/input.txt
generate input > arc001/B/input.txt
generate input > arc001/C/input.txt
generate input > arc001/D/input.txt

Directory structure after execution

> tree
.
├── arc001
│   ├── A
│   │   └── input.txt
│   ├── B
│   │   └── input.txt
│   ├── C
│   │   └── input.txt
│   └── D
│       └── input.txt
└── start.py

5 directories, 5 files

Check what was generated

> cat arc001/A/input.txt
9
131142143
---
20
12341234123412341234
---
4
1111

Recommended Posts

Python script to get a list of input examples for the AtCoder contest
Python: Get a list of methods for an object
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
Try to get the function list of Python> os package
Get the number of specific elements in a python list
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Try to get a list of breaking news threads in Python.
Get the value of a specific key up to the specified index in the dictionary list in Python
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
I tried to create a Python script to get the value of a cell in Microsoft Excel
[Python] Get a list of folders only
[Python] A program that rotates the contents of the list to the left
Python> sys.path> List of strings indicating the path to search for modules
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Linux] Command to get a list of commands executed in the past
Get the caller of a function in Python
Make a copy of the list in Python
A python amateur tries to summarize the list ②
How to pass the execution result of a shell command in a list in Python
Atcoder Beginner Contest A, B Input summary Python that tends to be a problem
I made a script to record the active window using win32gui of Python
Template of python script to read the contents of the file
How to get the number of digits in Python
[python] Get the list of classes defined in the module
Get the return code of the Python script from bat
[Python] Get the list of ExifTags names of Pillow library
[Maya Python] Crush the contents of the script 2 ~ list Notes
Script to get the expiration date of the SSL certificate
Python Note: The mystery of assigning a variable to a variable
A python script that generates a sample dataset for checking the operation of a classification tree
A python script that gets the number of jobs for a specified condition from indeed.com
Get the value of a specific key in a list from the dictionary type in the list with Python
How to check in Python if one of the elements of a list is in another list
AtCoder Beginner Contest 166 A Explanation of Problem "A? C" (Python3, C ++, Java)
[Python] How to make a list of character strings character by character
Review of AtCoder Beginner Contest 159, up to question E (Python)
Review of AtCoder Beginner Contest 163, up to question E (Python)
How to shuffle a part of a Python list (at random.shuffle)
[Maya Python] Crush the contents of the script 3 ~ List unknown Plugins
Review of AtCoder Beginner Contest 164, up to question E (Python)
[Command] Command to get a list of files containing double-byte characters
Review of AtCoder Beginner Contest 162, up to question E (Python)
Get a list of purchased DMM eBooks with Python + Selenium
Since Python 1.5 of Discord, I can't get a list of members
Review of AtCoder Beginner Contest 154, up to question E (Python)
AtCoder Grand Contest 046: A --Animation drawing of Takahashikun, The Strider
Get the number of occurrences for each element in the list
Review of AtCoder Beginner Contest 160, up to question E (Python)
Review of AtCoder Beginner Contest 167, up to question E (Python)
Review of AtCoder Beginner Contest 157, up to question E (Python)
Review of AtCoder Beginner Contest 161, up to question E (Python)
Review of AtCoder Beginner Contest 155, up to question E (Python)
Review of AtCoder Beginner Contest 156, up to question E (Python)
Review of AtCoder Beginner Contest 166, up to question E (Python)
How to get a list of links from a page from wikipedia
A Python script that compares the contents of two directories
Get the source of the page to load infinitely with python.
How to connect the contents of a list into a string