Python hand play (let's get started with AtCoder?)

What is this article?

I was interested in AtCoder and tried typing the code directly in the browser. So, of course, I wanted to move it locally and check it before uploading it. Then, half a year later, I would like to say to myself, "What, I didn't know this?", So I want to keep the sauce. (Well, locally.) Then, by the way, let's have a sample problem as a part of the code, test it, and wait until it's OK. So I added a mode of operation and created a framework (?) Tick function that is easy to evaluate.

History

I heard that there is a puzzle that seems to be interesting ... No, I knew the existence itself, but the content was too enthusiastic, and I thought it was a little bit like that, so I gave it up. However, when I heard that I could participate in AtCoder on Scratch, I said, "Well, isn't it a purveyor to experts?" .. ..

Then, I thought I could participate for the first time. Wrong. I thought I'd start.

Specification overview? Requirements?

Make one .py file that can be started independently Normal operation (for submission) with no arguments Enter 1, 2 etc. as arguments and output the test pattern execution result and OK / NG presented in the question sentence

code

Example 1: A --Welcome to AtCoder

■A - Welcome to AtCoder https://atcoder.jp/contests/practice/tasks/practice_1

 practice_1.py

# answer
def practice_1(lines):
    a = int(lines[0])
    b, c = map(int, lines[1].split(' '))
    s = lines[2]
    return [str(a+b+c) + ' ' + s]


# Get arguments
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# test data
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['1', '2 3', 'test']
        lines_export = ['6 test']
    elif pattern == 2:
        lines_input = ['72', '128 256', 'myonmyon']
        lines_export = ['456 myonmyon']
    return lines_input, lines_export


# Operation mode determination
def get_mode():
    import sys
    args = sys.argv
    if len(args) == 1:
        mode = 0
    else:
        mode = int(args[1])
    return mode


# Main processing
def main():
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(3)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = practice_1(lines_input)

    for line_result in lines_result:
        print(line_result)

    if mode > 0:
        print(f'lines_input=[{lines_input}]')
        print(f'lines_export=[{lines_export}]')
        print(f'lines_result=[{lines_result}]')
        if lines_result == lines_export:
            print('OK')
        else:
            print('NG')


# Start process
if __name__ == '__main__':
    main()

(Output result)

(base) ...>python practice_1.py 1
6 test
lines_input=[['1', '2 3', 'test']]
lines_export=[['6 test']]
lines_result=[['6 test']]
OK

(base) ...>python practice_1.py 2
456 myonmyon
lines_input=[['72', '128 256', 'myonmyon']]
lines_export=[['456 myonmyon']]
lines_result=[['456 myonmyon']]
OK

(base) ...>python practice_1.py
3
5 7
abc
15 abc



Example 2: ABC173: A --Payment

https://atcoder.jp/contests/abc173/tasks/abc173_a

 abc173_a.py

# answer
def abc173_a(lines):
    N = int(lines[0])
    amari = N % 1000
    if amari == 0:
        answer = 0
    else:
        answer = 1000 - amari
    return [answer]


# Get arguments
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# test data
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['1900']
        lines_export = [100]
    elif pattern == 2:
        lines_input = ['3000']
        lines_export = [0]
    return lines_input, lines_export


# Operation mode determination
def get_mode():
    import sys
    args = sys.argv
    if len(args) == 1:
        mode = 0
    else:
        mode = int(args[1])
    return mode


# Main processing
def main():
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = abc173_a(lines_input)

    for line_result in lines_result:
        print(line_result)

    if mode > 0:
        print(f'lines_input=[{lines_input}]')
        print(f'lines_export=[{lines_export}]')
        print(f'lines_result=[{lines_result}]')
        if lines_result == lines_export:
            print('OK')
        else:
            print('NG')


# Start process
if __name__ == '__main__':
    main()


(Output result)

(base) >python abc173_a.py 1
100
lines_input=[['1900']]
lines_export=[[100]]
lines_result=[[100]]
OK

(base) >python abc173_a.py 2
0
lines_input=[['3000']]
lines_export=[[0]]
lines_result=[[0]]
OK

(base) >python abc173_a.py
2380
620

(

Example 3: ABC173: B --Judge Status Summary

https://atcoder.jp/contests/abc173/tasks/abc173_b

 abc173_b.py

# answer
def abc173_b(lines):
    labels = ['AC', 'WA', 'TLE', 'RE']
    ns = [0, 0, 0, 0]
    for line in range(1, len(lines)):
        for i, label in enumerate(labels):
            if label == lines[line]:
                ns[i] += 1
    answers = list()
    for i, label in enumerate(labels):
        answers.append(label + ' x ' + str(ns[i]))
    return answers


# Get arguments
def get_input_lines_1stline_as_n_lines():
    lines = list()
    lines.append(input())
    for _ in range(int(lines[0])):
        lines.append(input())
    return lines


# test data
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['6', 'AC', 'TLE', 'AC', 'AC', 'WA', 'TLE']
        lines_export = ['AC x 3', 'WA x 1', 'TLE x 2', 'RE x 0']
    elif pattern == 2:
        lines_input = ['10', 'AC', 'AC', 'AC', 'AC', 'AC', 'AC', 'AC', 'AC', 'AC', 'AC']
        lines_export = ['AC x 10', 'WA x 0', 'TLE x 0', 'RE x 0']
    return lines_input, lines_export


# Operation mode determination
def get_mode():
    import sys
    args = sys.argv
    if len(args) == 1:
        mode = 0
    else:
        mode = int(args[1])
    return mode


# Main processing
def main():
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines_1stline_as_n_lines()
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = abc173_b(lines_input)

    for line_result in lines_result:
        print(line_result)

    if mode > 0:
        print(f'lines_input=[{lines_input}]')
        print(f'lines_export=[{lines_export}]')
        print(f'lines_result=[{lines_result}]')
        if lines_result == lines_export:
            print('OK')
        else:
            print('NG')


# Start process
if __name__ == '__main__':
    main()

(Output result)

(base) >python abc173_b.py 1
AC x 3
WA x 1
TLE x 2
RE x 0
lines_input=[['6', 'AC', 'TLE', 'AC', 'AC', 'WA', 'TLE']]
lines_export=[['AC x 3', 'WA x 1', 'TLE x 2', 'RE x 0']]
lines_result=[['AC x 3', 'WA x 1', 'TLE x 2', 'RE x 0']]
OK

(base) >python abc173_b.py 2
AC x 10
WA x 0
TLE x 0
RE x 0
lines_input=[['10', 'AC', 'AC', 'AC', 'AC', 'AC', 'AC', 'AC', 'AC', 'AC', 'AC']]
lines_export=[['AC x 10', 'WA x 0', 'TLE x 0', 'RE x 0']]
lines_result=[['AC x 10', 'WA x 0', 'TLE x 0', 'RE x 0']]
OK

(base) >python abc173_b.py
5
AC
AC
WA
TLE
TLE
AC x 2
WA x 1
TLE x 2
RE x 0

Impressions

Well, for the time being, this is it. With the attitude of trying to get started rather than making it Best. Maybe if I keep going, I'll make a lot of changes.

For the time being, Welcome has become AC as it is, so let's put this up from today. ABC173 read from A to F, but D didn't come out very well. F was confusing and didn't feel like reading. When I wrote the code for E, it became sober and annoying that I interrupted it. Well, I wonder if it's okay to get used to it after D. C was also a lot of trouble.

I'm wondering if my style will become a Better through logical assembly and thought organization, so I think I'd like to tackle the issues and take a little flow of watching the explanation video ... If you continue. .. ..

Recommended Posts

Python hand play (let's get started with AtCoder?)
[Blender x Python] Let's get started with Blender Python !!
Get started with Python! ~ ② Grammar ~
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Let's play with Excel with Python [Beginner]
How to get started with Python
Get started with Python in Blender
Let's get started with Python ~ Building an environment on Windows 10 ~
Play with 2016-Python
Get Started with TopCoder in Python (2020 Edition)
How Python beginners get started with Python with Progete
Let's get along with Python # 0 (Environment construction)
Django 1.11 started with Python3.6
Solve AtCoder 167 with python
1.1 Getting Started with Python
Getting Started with Python
[Cloud102] # 1 Let's get started with Python (Part 2 Jupyter Notebook Construction AWS Edition)
Get started with MicroPython
Get started with Python on macOS Big Sur
Get date with python
Get started with Mezzanine
Getting Started with Python
A layman wants to get started with Python
[Cloud102] # 1 Get Started with Python (Part 1 Python First Steps)
Python hand play (division)
[Cloud102] # 1 Let's get started with Python (Part 3 Jupyter Notebook Construction GCP Cloud Shell Edition)
I tried to get started with blender python script_Part 01
[Let's play with Python] Make a household account book
I tried to get started with blender python script_Part 02
[Piyopiyokai # 1] Let's play with Lambda: Get a Twitter account
Python hand play (get column names from CSV file)
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Get country code with python
Python hand play (two-dimensional list)
Let's play with 4D 4th
Getting Started with Python Functions
Let's play with Amedas data-Part 1
Solve AtCoder ABC166 with python
Get started with Django! ~ Tutorial ⑤ ~
Light blue with AtCoder @Python
Get Twitter timeline with python
Get started with influxDB + Grafana
Let's run Excel with Python
Get Youtube data with python
Getting Started with Python Django (1)
Get started with Django! ~ Tutorial ④ ~
Let's play with Amedas data-Part 4
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Get started with Django! ~ Tutorial ⑥ ~
[Python] Play with Discord's Webhook.
Let's write python with cinema4d.
Get thread ID with python
Play RocketChat with API / Python
Getting Started with Python Django (6)
Let's play with Amedas data-Part 3
Get stock price with Python
Let's play with Amedas data-Part 2
Let's build git-cat with Python
Get home directory with python