inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

파이썬 알고리즘 문제풀이 입문(코딩테스트 대비)

강의자료(Mac 사용자: 채점 프로그램은 없습니다)

mac 용 채점 스크립트

448

JaeWon Lee
1

mac 용 python 채점 스크립트입니다.

judge.py 파일로 만들어서 각 폴더에 위치시킨 후 커맨드창에서 python judge.py 실행하면 됩니다.

python 커맨드가 다른 경우에는 PYTHON_CMD 를 변경해주세요 (예: 명령어가 python3 인 경우)

import fileinput
import subprocess

PYTHON_CMD = "python"
TEST_FILE_NAME = "aa.py"

def get_content(file_path):
    contents = open(file_path).read()
    return contents.rstrip()

def run_command(input_file_path):
    return f"{PYTHON_CMD} {TEST_FILE_NAME} < {input_file_path}"

def run_test(input_file_path):
    result = subprocess.run(run_command(input_file_path), shell=True, stdout=subprocess.PIPE)
    result = result.stdout.decode("utf-8").rstrip()
    return result

# main
for i in range(1, 6):
    answer = get_content(f"out{i}.txt")
    result = run_test(f"in{i}.txt")

    if result == answer:
        print(f"Case #{i} : Success")
    else:
        print(f"Case #{i} : Fail")
        print("-------------------")
        print(f"result=\n{result}")
        print(f"answer=\n{answer}")
        print("-------------------")

답변 1

2

JaeWon Lee

mac 용 python 채점 스크립트입니다..

judge.py 파일로 각 폴더에 저장 후 커맨드창에서

python judge.py 실행하면 됩니다.

python 커맨드가 다른 경우에는 PYTHON_CMD 를 변경해주세요

import fileinput
import subprocess
from datetime import datetime

PYTHON_CMD = "python"
TEST_FILE_NAME = "aa.py"

def get_content(file_path):
    contents = open(file_path).read()
    return contents.rstrip()

def run_command(input_file_path):
    return f"{PYTHON_CMD} {TEST_FILE_NAME} < {input_file_path}"

def run_test(input_file_path):
    result = subprocess.run(run_command(input_file_path), shell=True, stdout=subprocess.PIPE)
    result = result.stdout.decode("utf-8").rstrip()
    return result

start_time = datetime.now()

# main
for i in range(1, 6):
    answer = get_content(f"out{i}.txt")
    result = run_test(f"in{i}.txt")

    if result == answer:
        print(f"Case #{i} : Success")
    else:
        print(f"Case #{i} : Fail")
        print("-------------------")
        print(f"result=\n{result}")
        print(f"answer=\n{answer}")
        print("-------------------")

end_time = datetime.now()
diff = end_time - start_time
print("elapsed_time(ms) : " + str(diff.microseconds / 1000))