LoginSignup
1
2

More than 3 years have passed since last update.

競技プログラミング、コーディングテストの雛形:Python3

Last updated at Posted at 2020-12-19

目的

  • コーディングテスト、競技プログラミングで使える雛形を載せておきます
  • 要件
    • Python3
    • 提出前にデバッグするときは、テストできること(複数パターンをテストできること)
    • 提出前にデバッグするときは、ログを出せること
    • 提出用のコードを作るときに、なるべく手作業が少ないこと

雛形

  • 雛形では足し算を実装して、2パターンテストしています
  • 説明はコードコメントにて
  • 提出時には、inputters = [UtilityForRelease()]に手作業で切り替えてください(コード参照)
"""
    コーディングテスト用の雛形
"""

from abc import ABCMeta, abstractmethod

# 無くてもいいが、意図が伝わりやすいかと思って入れている
class IUtility(metaclass=ABCMeta):
    """ユーティリティのインタフェースクラス
    """
    @abstractmethod
    def input(self):
        pass
    @abstractmethod
    def check(self, test_outputs):
        pass
    @abstractmethod
    def debug(self, message):
        pass

class UtilityForRelease(IUtility):
    """本番用のユーティリティ
    """
    def input(self):
        """
            標準入力から1行返す
           Returns:
                string: 標準入力
        """
        return input()
    def check(self, test_outputs):
        """
            何もしない
        """
        pass
    def debug(self, messsage):
        """
            何もしない
        """
        pass

class UtilityForDebug(IUtility):
    """デバッグ用のユーティリティ
    """
    def __init__(self, test_inputs, test_answers):
        """コンストラクタ
            Args:
                test_inputs (array of string): 入力文字列の配列(文字列であることに注意!!)
                test_answers (array of string): 正解文字列の配列(文字列であることに注意!!)
        """
        self.test_inputs = test_inputs
        self.test_answers = test_answers
        self.counter = 0

    def input(self):
        """
            コンストラクタで与えられた文字列を1行返す
            Returns:
                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):
        """
            コンストラクタで与えれた答えと照合する
            Args:
                test_outputs (array of 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):
        """
            ログを出力する
        """
        print("DEBUG::" + message)

def main():
    """ エントリポイント
    """

    # 提出時とデバッグ時で下を切り替えて使ってください!!
    # デバッグ時には入力と正解を引数にUtilityForDebugを生成してください
    inputters = [UtilityForDebug(["1", "2"], ["3"]), UtilityForDebug(["3", "4"], ["7"])]
    # inputters = [UtilityForRelease()]

    for inputter in inputters:
        # 以下に入力を取得して、ロジックを実装し、出力するところまで記述してください
        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()
  • デバッグ時の結果です
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
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2