朱东升 commited on
Commit
3499425
·
1 Parent(s): f809c05

requirements update14

Browse files
Files changed (2) hide show
  1. app.py +46 -46
  2. test.py +68 -0
app.py CHANGED
@@ -4,6 +4,7 @@ import importlib
4
  import os
5
  import sys
6
  from pathlib import Path
 
7
 
8
  # 添加当前目录和src目录到模块搜索路径
9
  current_dir = os.path.dirname(os.path.abspath(__file__))
@@ -17,23 +18,32 @@ def evaluate(input_data):
17
  """评估代码的主函数
18
 
19
  Args:
20
- input_data: 可以是字符串(文件路径)、字典(包含代码信息)或列表(批量处理多个测试用例)
21
 
22
  Returns:
23
- dict/list: 包含评估结果的字典或列表
24
  """
25
  try:
26
- # 如果输入是列表(批量处理)
27
- if isinstance(input_data, list):
28
- results = []
29
- for item in input_data:
30
- result = evaluate_single_case(item)
31
- item.update(result)
32
- results.append(item)
33
- return results
34
-
35
- # 单个用例处理
36
- return evaluate_single_case(input_data)
 
 
 
 
 
 
 
 
 
37
 
38
  except Exception as e:
39
  return {"status": "Exception", "error": str(e)}
@@ -42,47 +52,37 @@ def evaluate_single_case(input_data):
42
  """评估单个代码用例
43
 
44
  Args:
45
- input_data: 可以是字符串(文件路径)或字典(包含代码信息)
46
 
47
  Returns:
48
  dict: 包含评估结果的字典
49
  """
50
  try:
51
- # 如果输入是文件路径
52
- if isinstance(input_data, str):
53
- with open(input_data, 'r') as f:
54
- code = f.read()
55
- # 从文件扩展名确定语言
56
- language = Path(input_data).suffix[1:]
57
- result = evaluate_code(code, language)
58
- return result
59
-
60
- # 如果输入是字典
61
- elif isinstance(input_data, dict):
62
- language = input_data.get('language')
63
- completions = input_data.get('processed_completions', [])
64
 
65
- if not completions:
66
- return {"status": "Exception", "error": "No code provided"}
67
 
68
- # 评估所有完成的代码
69
- results = []
70
- for comp in completions:
71
- code = input_data.get('prompt') + comp + '\n' + input_data.get('tests')
72
- result = evaluate_code(code, language)
73
- # 如果当前代码执行成功,立即返回pass,不再评估后续代码
74
- if result["status"] == "OK":
75
- return {"status": "pass"}
76
- print(f'Code failed to compile: \n{code}')
77
- result['compiled_code'] = code
78
- results.append(result)
79
-
80
- # 所有代码都执行失败,返回第一个失败结果
81
- return results[0]
82
-
83
- else:
84
- return {"status": "Exception", "error": "Invalid input format"}
85
 
 
 
 
86
  except Exception as e:
87
  return {"status": "Exception", "error": str(e)}
88
 
 
4
  import os
5
  import sys
6
  from pathlib import Path
7
+ import concurrent.futures
8
 
9
  # 添加当前目录和src目录到模块搜索路径
10
  current_dir = os.path.dirname(os.path.abspath(__file__))
 
18
  """评估代码的主函数
19
 
20
  Args:
21
+ input_data: 列表(批量处理多个测试用例)
22
 
23
  Returns:
24
+ list: 包含评估结果的列表
25
  """
26
  try:
27
+ # 确保输入是列表
28
+ if not isinstance(input_data, list):
29
+ return {"status": "Exception", "error": "Input must be a list"}
30
+
31
+ results = []
32
+ # 使用线程池并行处理多个测试用例
33
+ with concurrent.futures.ThreadPoolExecutor() as executor:
34
+ # 提交所有任务到线程池
35
+ future_to_item = {executor.submit(evaluate_single_case, item): item for item in input_data}
36
+ # 获取结果
37
+ for future in concurrent.futures.as_completed(future_to_item):
38
+ item = future_to_item[future]
39
+ try:
40
+ result = future.result()
41
+ item.update(result)
42
+ results.append(item)
43
+ except Exception as e:
44
+ item.update({"status": "Exception", "error": str(e)})
45
+ results.append(item)
46
+ return results
47
 
48
  except Exception as e:
49
  return {"status": "Exception", "error": str(e)}
 
52
  """评估单个代码用例
53
 
54
  Args:
55
+ input_data: 字典(包含代码信息)
56
 
57
  Returns:
58
  dict: 包含评估结果的字典
59
  """
60
  try:
61
+ # 只处理字典类型输入
62
+ if not isinstance(input_data, dict):
63
+ return {"status": "Exception", "error": "Input item must be a dictionary"}
64
+
65
+ language = input_data.get('language')
66
+ completions = input_data.get('processed_completions', [])
 
 
 
 
 
 
 
67
 
68
+ if not completions:
69
+ return {"status": "Exception", "error": "No code provided"}
70
 
71
+ # 评估所有完成的代码
72
+ results = []
73
+ for comp in completions:
74
+ code = input_data.get('prompt') + comp + '\n' + input_data.get('tests')
75
+ result = evaluate_code(code, language)
76
+ # 如果当前代码执行成功,立即返回pass,不再评估后续代码
77
+ if result["status"] == "OK":
78
+ return {"status": "pass"}
79
+ print(f'Code failed to compile: \n{code}')
80
+ result['compiled_code'] = code
81
+ results.append(result)
 
 
 
 
 
 
82
 
83
+ # 所有代码都执行失败,返回第一个失败结果
84
+ return results[0]
85
+
86
  except Exception as e:
87
  return {"status": "Exception", "error": str(e)}
88
 
test.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import defaultdict
2
+
3
+ def remove_longest_prefix(A, B, n):
4
+ len_A, len_B = len(A), len(B)
5
+
6
+ if n >= len_A:
7
+ return B[len_A:] if len_A <= len_B else B
8
+
9
+ max_match = 0
10
+ best_pos = 0
11
+
12
+ # 构建字符位置映射表(包含偏移量限制)
13
+ char_index = defaultdict(list)
14
+ max_offset = min(len_A + n, len_B) # 添加偏移量限制
15
+ for i, c in enumerate(B[:max_offset]):
16
+ char_index[c].append(i)
17
+
18
+ # 逆向扫描关键字符
19
+ for a_pos in reversed(range(len_A)):
20
+ target_char = A[a_pos]
21
+ if target_char not in char_index:
22
+ continue
23
+
24
+ # 检查可能匹配的位置范围
25
+ for b_start in char_index[target_char]:
26
+ # 增加位置有效性验证
27
+ if b_start < max(0, a_pos - n) or b_start > min(len_B, a_pos + n):
28
+ continue
29
+
30
+ # 计算动态窗口范围
31
+ window_start = max(0, b_start - a_pos)
32
+ window_size = min(a_pos + n + 1, len_A, len_B - window_start)
33
+
34
+ # 初始化DP数组(修复索引问题)
35
+ prev_row = list(range(window_start, window_start + window_size + 1))
36
+
37
+ # 动态规划计算(添加边界保护)
38
+ for i in range(1, window_size + 1):
39
+ current_row = [prev_row[0] + 1] # 第一个元素单独处理
40
+ for j in range(1, window_size + 1):
41
+ # 添加索引有效性检查
42
+ if (window_start + j - 1) >= len_B:
43
+ break
44
+
45
+ cost = 0 if A[i-1] == B[window_start + j -1] else 1
46
+ current_row.append(min(
47
+ prev_row[j-1] + cost,
48
+ prev_row[j] + 1 if j < len(prev_row) else float('inf'),
49
+ current_row[j-1] + 1
50
+ ))
51
+ # 提前终止条件
52
+ if current_row[-1] > n:
53
+ break
54
+ prev_row = current_row
55
+ if all(v > n for v in current_row):
56
+ break
57
+
58
+ # 更新最佳匹配(添加有效性验证)
59
+ if window_size > 0 and prev_row[-1] <= n and (a_pos +1) > max_match:
60
+ max_match = a_pos +1
61
+ best_pos = window_start + window_size
62
+
63
+ return B[best_pos:] if max_match >0 else B
64
+
65
+ A = "#include<assert.h>\n#include<bits/stdc++.h>\n// Check if in given vector of numbers, are any two numbers closer to each other than\n// given threshold.\n// >>> has_close_elements((std::vector<float>({(float)1.0f, (float)2.0f, (float)3.0f})), (0.5f))\n// (false)\n// >>> has_close_elements((std::vector<float>({(float)1.0f, (float)2.8f, (float)3.0f, (float)4.0f, (float)5.0f, (float)2.0f})), (0.3f))\n// (true)\nbool has_close_elements(std::vector<float> numbers, float threshold) {\n"
66
+ B = "#include <assert.h>\n#include <bits/stdc++.h>\n\n// Check if in given vector of numbers, are any two numbers closer to each other than\n// given threshold.\n// >>> has_close_elements((std::vector<float>({(float)1.0f, (float)2.0f, (float)3.0f})), (0.5f))\n// (false)\n// >>> has_close_elements((std::vector<float>({(float)1.0f, (float)2.8f, (float)3.0f, (float)4.0f, (float)5.0f, (float)2.0f})), (0.3f))\n// (true)\nbool has_close_elements(std::vector<float> numbers, float threshold) {\n // Sort the vector in ascending order\n std::sort(numbers.begin(), numbers.end());\n\n // Iterate over the sorted vector\n for (size_t i = 0; i < numbers.size() - 1; ++i) {\n // Check if the difference between the current element and the next element is less than the threshold\n if (numbers[i + 1] - numbers[i] < threshold) {\n return true; // If a pair of elements is found that are closer than the threshold, return true\n }\n }\n\n // If no pair of elements is found that are closer than the threshold, return false\n return false;\n}\n\nint main() {\n std::vector<float> numbers1 = {1.0f, 2.0f, 3.0f};\n std::vector<float> numbers2 = {1.0f, 2.8f, 3.0f, 4.0f, 5.0f, 2.0f};\n\n std::cout << std::boolalpha << has_close_elements(numbers1, 0.5f) << std::endl; // Output: false\n std::cout << std::boolalpha << has_close_elements(numbers2, 0.3f) << std::endl; // Output: true\n\n return 0;\n}\n"
67
+ n = 5
68
+ print(remove_longest_prefix(A, B, n))