judge hungyen22

04/05/2023

requirement: python

Code
import subprocess
from pathlib import Path
import sys
import os


id = input("Nhap id bai: ").upper()
while (not id in ['CUT', 'FAIL', 'PIN', 'POST', 'RICHNUM']):
	id = input("Khong hop le, vui long nhap lai id bai: ").upper()

codefile = input("Nhap ten file: ")
lang = input("Nhap ngon ngu: ")
while (not lang in ['cpp']):
	lang = input("Khong hop le, vui long nhap lai ngon ngu: ")

file_path = Path(f"{codefile}.{lang}")
filename = codefile
if not file_path.exists():
	print(f"File {file_path} không tồn tại")
	exit()

if lang == "cpp":
	cmd = f"g++ {file_path} -o {filename}.exe"

subprocess.run(cmd)

print("---------")
res = 0
cnt = 1
problem_path = Path(f"./{id}/")

for subdir, dirs, files in os.walk(problem_path):
	inp_content = ""
	ans_content = ""

	for dir in dirs:
		#doc inp, ans
		with open(f"./{id}/{dir}/" + f"{id}.INP", "r") as input_file:
			inp_content = input_file.read()
		with open(f"./{id}/{dir}/" + f"{id}.OUT", "r") as output_file:
			ans_content = output_file.read()

		with open(f"{id}.inp", "w") as f:
		   	f.write(inp_content)
		try:
			subprocess.run(f"{filename}.exe", timeout=1)
		except subprocess.TimeoutExpired:
			print(f"{dir}: TLE!")
			sys.exit(1)
		except subprocess.CalledProcessError:
			print(f"{dir}: RTE!")
			sys.exit(1)

		out_file = Path(f"{id}.out")
		if not out_file.exists():
			print("Lỗi: không tìm thấy file output")
			exit()

		out_content = out_file.read_text().strip()

		if out_content == ans_content.strip():
			print(f"{dir}: passed!")
			res += 1
		else:
			print(f"{dir}: failed!")
		cnt += 1

print(f"---------\nKết quả: {res}/{cnt-1}")