Skip to content

Instantly share code, notes, and snippets.

@xsohn
Last active October 17, 2018 05:09
Show Gist options
  • Save xsohn/54e0b6e11908cc35427adbdc5535ecd2 to your computer and use it in GitHub Desktop.
Save xsohn/54e0b6e11908cc35427adbdc5535ecd2 to your computer and use it in GitHub Desktop.
2018-IT개론 자동채점 스크립트
from os import listdir
from os.path import isfile, join
import subprocess
# 채점할 코드와 같은 폴더에 있어야 됨
# 테스트 케이스는 "test1_1"형식으로 같은 폴더에 저장한다. [.txt 없음]
# ex) 문제1 테스트 케이스1 : test1_1, 문제3 테스트 케이스5 : test3_5
# 채점할 코드의 파일명은 P?_????????_!.py 로 생각하고, '_'로 split해서,
# 문제번호에 해당하는 !를 알아내는 구조
problem_cnt = int(input('num of problems:'))
test_case_cnt = [0]
for i in range(problem_cnt):
x = int(input('num of testcase of problem #%d:' % (i+1)))
test_case_cnt.append(x)
mypath = './'
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
files_cnt = len(files)
files.sort()
outputs = ""
prev = 0
for f in files:
if f.find('.py') == -1 or f == "grade.py":
continue
tmp = f.split('_')
tmp2 = tmp[2].split('.')
student_num = tmp[1]
problem_num = int(tmp2[0])
if prev != student_num:
outputs += "========================["+student_num+"]========================\n"
for i in range(test_case_cnt[problem_num]):
try:
result = subprocess.check_output("python3 "+f+" < "+"test"+str(problem_num)+"_%d"%(i+1), shell=True)
except subprocess.CalledProcessError as e:
result = e.output
outputs += "# "+str(problem_num)+"_%d"%(i+1)+"\n\n"
outputs += result.decode("utf-8") + "\n"
prev = student_num
with open("result", "w") as f:
f.write(outputs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment