맥 채점코드

mail@pastecode.io avatar
unknown
python
3 years ago
1.4 kB
26
Indexable
Never
import os
import sys
import runpy

# 사용방법 my.py에 해답을 작성하고, test.py를 실행하면
# 통과시에 pass 실패시에 Faile 정답과 당신의 틀린 답을 같이 출력해줍니다.
print(__file__)
folder = os.path.dirname(__file__)
source_file_name = os.path.join(folder, 'my.py')
for file_name in os.listdir(folder):
    if file_name.endswith('.txt') and file_name.startswith('in'):
        print('file_name', file_name, end=": ")
        sys.stdin = open(os.path.join(folder, file_name), 'rt')
        my_file_name = file_name.replace('in', 'my')
        sys.stdout = open(os.path.join(folder, my_file_name), 'wt')
        runpy.run_path(source_file_name)
        out_file_name = file_name.replace('in', 'out')
        out_file = open(os.path.join(folder, out_file_name), 'rt')
        my_file = open(os.path.join(folder, my_file_name), 'rt')
        # 표준 출력을 다시 원래 되로 돌린다.
        sys.stdout = sys.__stdout__
        while True:
            # rstrip을 뒤에 혹시오는 \n 뉴라인 문자 제거
            out_line = out_file.readline().rstrip()
            my_line = my_file.readline().rstrip()
            if len(out_line) == 0:
                break
            if out_line != my_line:
                print(f'Failed. out:{out_line} my:{my_line}')
                exit()
        print('Pass')