@@ -10,27 +10,50 @@ def judge():
1010 stdout = input_data ["stdout" ]
1111 max_score = input_data ["max_score" ]
1212
13- # 查找 shell 提示符模式,以便正确识别命令输出
14- prompt_pattern = r"[\w\d\-_]+[@:][\w\d\-_/]+[#$%>]"
15- prompt_matches = re .findall (prompt_pattern , stdout )
16- prompt = None
17- if prompt_matches :
18- # 使用最常见的提示符
19- from collections import Counter
13+ prompt = "> "
14+ success = True
15+ score = 0
16+
17+ # 分析输出,查找命令和它们的输出
18+ commands_with_output = []
19+
20+ # 使用提示符分割输出
21+ lines = stdout .split ("\n " )
22+ prompt_line_indices = []
23+
24+ # 找出所有提示符所在行的索引
25+ for i , line in enumerate (lines ):
26+ if line .startswith (prompt .strip ()):
27+ prompt_line_indices .append (i )
28+
29+ # 解析每个命令和它的输出
30+ for i , prompt_index in enumerate (prompt_line_indices ):
31+ # 获取命令
32+ cmd_line = lines [prompt_index ]
33+ cmd = cmd_line [len (prompt .strip ()) :].strip ()
2034
21- prompt = Counter (prompt_matches ).most_common (1 )[0 ][0 ]
35+ # 获取输出: 从命令行的下一行开始,到下一个提示符行(或文件结束)
36+ output_start = prompt_index + 1
37+ output_end = (
38+ prompt_line_indices [i + 1 ]
39+ if i + 1 < len (prompt_line_indices )
40+ else len (lines )
41+ )
42+
43+ # 如果没有输出行或者下一行就是提示符,则输出为空
44+ if output_start >= output_end :
45+ output = ""
46+ else :
47+ output = "\n " .join (lines [output_start :output_end ])
48+
49+ commands_with_output .append ((cmd , output ))
2250
2351 # 提取 pwd 命令的输出
2452 pwd_outputs = []
25- if prompt :
26- # 查找 pwd 命令的输出
27- pwd_pattern = rf"{ re .escape (prompt )} pwd\s*\n(.*?)(?={ re .escape (prompt )} |$)"
28- pwd_matches = re .findall (pwd_pattern , stdout , re .DOTALL )
29- pwd_outputs = [match .strip () for match in pwd_matches ]
30- else :
31- # 如果找不到提示符,尝试直接提取可能的路径
32- path_pattern = r"/[a-zA-Z0-9_\-./]+"
33- pwd_outputs = re .findall (path_pattern , stdout )
53+
54+ for cmd , output in commands_with_output :
55+ if cmd .strip () == "pwd" :
56+ pwd_outputs .append (output .strip ())
3457
3558 # 检查是否有至少两个不同的目录输出(初始目录和 cd 后的目录)
3659 success = False
@@ -44,17 +67,17 @@ def judge():
4467 try :
4568 first_dir = os .path .normpath (pwd_outputs [0 ])
4669 second_dir = os .path .normpath (pwd_outputs [1 ])
47- parent_dir = os .path .normpath (os .path .join (first_dir , ".." ))
70+
71+ # 支持中文路径,确保正确处理路径分隔符
72+ parent_dir = os .path .normpath (os .path .dirname (first_dir ))
4873
4974 if second_dir == parent_dir :
5075 success = True
5176 message = "Directory change verified successfully"
5277 score = max_score
5378 else :
5479 message = f"Second directory ({ second_dir } ) is not the parent of first directory ({ first_dir } )"
55- score = (
56- max_score * 0.5
57- ) # 部分得分,因为目录确实改变了,但不是预期的父目录
80+ score = max_score * 0.5 # 部分得分,因为目录确实改变了,但不是预期的父目录
5881 except Exception as e :
5982 message = f"Error comparing directories: { str (e )} "
6083 else :
0 commit comments