upgrade to python3;add some validator examples
This commit is contained in:
58
examples/multi_solution_validator.py
Normal file
58
examples/multi_solution_validator.py
Normal file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
多解答案验证程序
|
||||
适用于有多个正确答案的题目,如排序、排列等
|
||||
使用: python multi_solution_validator.py input.txt expected.txt result.txt
|
||||
"""
|
||||
|
||||
import sys
|
||||
import re
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 4:
|
||||
print("WA")
|
||||
return
|
||||
|
||||
try:
|
||||
# 读取标准答案
|
||||
with open(sys.argv[2], 'r') as f:
|
||||
expected = f.read().strip()
|
||||
|
||||
# 读取用户输出
|
||||
with open(sys.argv[3], 'r') as f:
|
||||
result = f.read().strip()
|
||||
|
||||
# 去除多余的空白字符
|
||||
expected = re.sub(r'\s+', ' ', expected)
|
||||
result = re.sub(r'\s+', ' ', result)
|
||||
|
||||
# 解析数字
|
||||
try:
|
||||
expected_nums = list(map(int, expected.split()))
|
||||
result_nums = list(map(int, result.split()))
|
||||
except ValueError:
|
||||
print("WA")
|
||||
return
|
||||
|
||||
# 检查数量是否相同
|
||||
if len(expected_nums) != len(result_nums):
|
||||
print("WA")
|
||||
return
|
||||
|
||||
# 检查是否包含相同的元素(允许不同的排列)
|
||||
if sorted(expected_nums) != sorted(result_nums):
|
||||
print("WA")
|
||||
return
|
||||
|
||||
# 如果需要检查特定的排序顺序,可以添加额外的验证
|
||||
# 这里我们接受任何包含相同元素的排列
|
||||
print("AC")
|
||||
|
||||
except Exception:
|
||||
print("WA")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user