――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.
--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