Competitive programming, coding test template: Python3

Purpose

――I will post a template that can be used for coding tests and competitive programming. --Requirements - Python3 --When debugging before submission, you can test (you can test multiple patterns) --When debugging before submission, be able to log --When creating the code for submission, there should be as little manual work as possible.

Template

--In the template, addition is implemented and 2 patterns are tested. --Explanation in code comment --When submitting, manually switch to inputters = [UtilityForRelease ()] (see code)

"""
Template for coding test
"""

from abc import ABCMeta, abstractmethod

#You don't have to, but I think it's easy to convey your intentions.
class IUtility(metaclass=ABCMeta):
    """Utility interface class
    """
    @abstractmethod
    def input(self):
        pass
    @abstractmethod
    def check(self, test_outputs):
        pass
    @abstractmethod
    def debug(self, message):
        pass

class UtilityForRelease(IUtility):
    """Production utility
    """
    def input(self):
        """
Returns one line from standard input
           Returns:
                string:Standard input
        """
        return input()
    def check(self, test_outputs):
        """
do nothing
        """
        pass
    def debug(self, messsage):
        """
do nothing
        """
        pass

class UtilityForDebug(IUtility):
    """Debugging utility
    """
    def __init__(self, test_inputs, test_answers):
        """constructor
            Args:
                test_inputs (array of string):Array of input strings(Note that it is a string! !!)
                test_answers (array of string):Array of correct strings(Note that it is a string! !!)
        """
        self.test_inputs = test_inputs
        self.test_answers = test_answers
        self.counter = 0

    def input(self):
        """
Returns one line of the string given in the constructor
            Returns:
                string:Input string
        """
        test_input = self.test_inputs[self.counter]
        self.debug("input{0}: {1}".format(self.counter, test_input))
        self.counter += + 1
        return test_input

    def check(self, test_outputs):
        """
Collate with the answer given in the constructor
            Args:
                test_outputs (array of string):Array of output strings(Note that it is a string! !!)
        """
        self.debug("test: expected {0}, output {1}".format(self.test_answers, test_outputs))

        if self.test_answers == test_outputs:
            self.debug("test: result: TRUE");
        else:
            self.debug("test: result: ******FALSE******");
        
    def debug(self, message):
        """
Output log
        """
        print("DEBUG::" + message)

def main():
    """Entry point
    """

    #Please switch between the bottom when submitting and when debugging! !!
    #When debugging, please generate UtilityForDebug with input and correct answer as arguments
    inputters = [UtilityForDebug(["1", "2"], ["3"]), UtilityForDebug(["3", "4"], ["7"])]
    # inputters = [UtilityForRelease()]

    for inputter in inputters:
        #Get the input below, implement the logic, and describe up to the output
        left = int(inputter.input())
        right = int(inputter.input())
        answer = left + right
        inputter.debug("answer: {0}".format(answer))
        inputter.check([str(answer)])
        print(answer)

if __name__ == "__main__":
    main()

--Results during debugging

DEBUG::input0: 1
DEBUG::input1: 2
DEBUG::answer: 3
DEBUG::test: expected ['3'], output ['3']
DEBUG::test: result: TRUE
3
DEBUG::input0: 3
DEBUG::input1: 4
DEBUG::answer: 7
DEBUG::test: expected ['7'], output ['7']
DEBUG::test: result: TRUE
7

Recommended Posts

Competitive programming, coding test template: Python3
Competitive Pro Template (Python)
Competitive programming diary python 20201213
Competitive programming diary python 20201220
Competitive programming with python
python unit test template
Competitive programming diary python
Python template for Codeforces-manual test-
Python Competitive Programming Site Summary
[Python] Competitive template [At Coder]
Python3 standard input for competitive programming
Competitive programming with python Local environment settings
[Competitive programming] [Python3] Required knowledge, for myself
Python programming note
Programming in python
Python Integrity Test
python argparse template
First Python ~ Coding 2 ~
[Python] Tkinter template
I made a competitive programming glossary with Python
Primality test with Python
I tried competitive programming
3. 3. AI programming with Python
Python basics 8 numpy test
Python test package memo
Primality test with python
Python programming with Atom
Python data analysis template
Jinja2 | Python template engine
Tips you should know when programming competitive programming with Python2
Python programming in Excel
LEGO Mindstorms 51515 Python Programming
[Python] Dynamic programming ABC015D
python tag integration test
Python template engine empy
Programming with Python Flask
Knowledge you need to know when programming competitive programming with Python2
I tried programming the chi-square test in Python and Java.
Programming with Python and Tkinter
Python3 programming functions personal summary
Atcoder Acing Programming Contest Python
[Python] Dynamic programming knapsack problem
Algorithm in Python (primality test)
[Python] Dynamic programming TDPC D
Python web programming article summary
Paiza Python Primer 1 Learn Programming
Python Machine Learning Programming> Keywords
Competitive programming is what (bonus)
Python Programming Workshop-Super Introductory Vol.4
Python debug and test module
Story of trying competitive programming 2
An introduction to Python Programming
[Python] Dynamic programming TDPC A
Network programming with Python Scapy
Set python test in jenkins
Python Design Pattern --Template method
Tips you should know when programming competitive programming with Python2 (useful library)
Re: Competitive Programming Life Starting from Zero Chapter 1.2 "Python of Tears"