Spaces:
Sleeping
Sleeping
朱东升
commited on
Commit
·
5491a9c
1
Parent(s):
27f86cc
requirements update20
Browse files- src/eval_adb.py +2 -2
- src/eval_clj.py +1 -1
- src/eval_cpp.py +2 -2
- src/eval_cs.py +1 -1
- src/eval_dart.py +1 -1
- src/eval_dfy.py +1 -1
- src/eval_dlang.py +1 -1
- src/eval_elixir.py +1 -1
- src/eval_fs.py +1 -1
- src/eval_go.py +1 -1
- src/eval_hs.py +1 -1
- src/eval_java.py +2 -2
- src/eval_julia.py +1 -1
- src/eval_lean.py +1 -1
- src/eval_lua.py +1 -1
- src/eval_luau.py +1 -1
- src/eval_matlab.py +1 -1
- src/eval_ocaml.py +1 -1
- src/eval_php.py +1 -1
- src/eval_pl.py +1 -1
- src/eval_python.py +1 -1
- src/eval_racket.py +1 -1
- src/eval_ruby.py +1 -1
- src/eval_rust.py +1 -1
- src/eval_scala.py +1 -1
- src/eval_sh.py +1 -1
- src/eval_swift.py +1 -1
- src/eval_ts.py +1 -1
- src/eval_v.py +1 -1
- src/libeval.py +40 -0
- src/safe_subprocess/module_test.py +1 -1
- test.py +41 -60
src/eval_adb.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
-
from generic_eval import main
|
4 |
|
5 |
|
6 |
LANG_NAME = "Ada"
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
+
from .generic_eval import main
|
4 |
|
5 |
|
6 |
LANG_NAME = "Ada"
|
src/eval_clj.py
CHANGED
@@ -3,7 +3,7 @@ Evaluates a generated Clojure program (.clj).
|
|
3 |
"""
|
4 |
import os
|
5 |
from pathlib import Path
|
6 |
-
from safe_subprocess import run
|
7 |
from libeval import run_without_exn
|
8 |
|
9 |
|
|
|
3 |
"""
|
4 |
import os
|
5 |
from pathlib import Path
|
6 |
+
from .safe_subprocess import run
|
7 |
from libeval import run_without_exn
|
8 |
|
9 |
|
src/eval_cpp.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
-
from generic_eval import main
|
4 |
|
5 |
LANG_NAME = "C++"
|
6 |
LANG_EXT = ".cpp"
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
+
from .generic_eval import main
|
4 |
|
5 |
LANG_NAME = "C++"
|
6 |
LANG_EXT = ".cpp"
|
src/eval_cs.py
CHANGED
@@ -6,7 +6,7 @@ import os
|
|
6 |
import subprocess
|
7 |
import tempfile
|
8 |
from pathlib import Path
|
9 |
-
from generic_eval import main
|
10 |
|
11 |
LANG_NAME = "CSharp"
|
12 |
LANG_EXT = ".cs"
|
|
|
6 |
import subprocess
|
7 |
import tempfile
|
8 |
from pathlib import Path
|
9 |
+
from .generic_eval import main
|
10 |
|
11 |
LANG_NAME = "CSharp"
|
12 |
LANG_EXT = ".cs"
|
src/eval_dart.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
|
5 |
def eval_script(path: Path):
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
|
5 |
def eval_script(path: Path):
|
src/eval_dfy.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
# 0 – success
|
5 |
# 1 – invalid command-line arguments
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
# 0 – success
|
5 |
# 1 – invalid command-line arguments
|
src/eval_dlang.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
from pathlib import Path
|
4 |
-
from safe_subprocess import run
|
5 |
import sys
|
6 |
import re
|
7 |
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
from pathlib import Path
|
4 |
+
from .safe_subprocess import run
|
5 |
import sys
|
6 |
import re
|
7 |
|
src/eval_elixir.py
CHANGED
@@ -2,7 +2,7 @@ import argparse
|
|
2 |
from sys import exit
|
3 |
import subprocess
|
4 |
from pathlib import Path
|
5 |
-
from generic_eval import main as gmain
|
6 |
|
7 |
|
8 |
def eval_script(path: Path):
|
|
|
2 |
from sys import exit
|
3 |
import subprocess
|
4 |
from pathlib import Path
|
5 |
+
from .generic_eval import main as gmain
|
6 |
|
7 |
|
8 |
def eval_script(path: Path):
|
src/eval_fs.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["dotnet", "fsi", "-d:DEBUG", str(path)])
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["dotnet", "fsi", "-d:DEBUG", str(path)])
|
src/eval_go.py
CHANGED
@@ -2,7 +2,7 @@ import argparse
|
|
2 |
from sys import exit
|
3 |
import subprocess
|
4 |
from pathlib import Path
|
5 |
-
from generic_eval import main as gmain
|
6 |
|
7 |
|
8 |
def eval_script(path: Path):
|
|
|
2 |
from sys import exit
|
3 |
import subprocess
|
4 |
from pathlib import Path
|
5 |
+
from .generic_eval import main as gmain
|
6 |
|
7 |
|
8 |
def eval_script(path: Path):
|
src/eval_hs.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["runghc", str(path)])
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["runghc", str(path)])
|
src/eval_java.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import os
|
2 |
import tempfile
|
3 |
-
from safe_subprocess import run
|
4 |
from pathlib import Path
|
5 |
-
from generic_eval import main
|
6 |
|
7 |
LANG_NAME = "Java"
|
8 |
LANG_EXT = ".java"
|
|
|
1 |
import os
|
2 |
import tempfile
|
3 |
+
from .safe_subprocess import run
|
4 |
from pathlib import Path
|
5 |
+
from .generic_eval import main
|
6 |
|
7 |
LANG_NAME = "Java"
|
8 |
LANG_EXT = ".java"
|
src/eval_julia.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from safe_subprocess import run
|
2 |
from pathlib import Path
|
3 |
|
4 |
def eval_script(path: Path):
|
|
|
1 |
+
from .safe_subprocess import run
|
2 |
from pathlib import Path
|
3 |
|
4 |
def eval_script(path: Path):
|
src/eval_lean.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
import subprocess
|
4 |
|
5 |
def eval_script(path: Path):
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
import subprocess
|
4 |
|
5 |
def eval_script(path: Path):
|
src/eval_lua.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["lua", str(path)])
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["lua", str(path)])
|
src/eval_luau.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
|
5 |
def eval_script(path: Path):
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
|
5 |
def eval_script(path: Path):
|
src/eval_matlab.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
def eval_script(path):
|
5 |
# Matlab has the requirement that all functions must appear at the end
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
def eval_script(path):
|
5 |
# Matlab has the requirement that all functions must appear at the end
|
src/eval_ocaml.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["ocaml", str(path)])
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["ocaml", str(path)])
|
src/eval_php.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
LANG_NAME = "PHP"
|
5 |
LANG_EXT = ".php"
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
LANG_NAME = "PHP"
|
5 |
LANG_EXT = ".php"
|
src/eval_pl.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["perl", path])
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["perl", path])
|
src/eval_python.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["python3", str(path)])
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
def eval_script(path: Path):
|
5 |
r = run(["python3", str(path)])
|
src/eval_racket.py
CHANGED
@@ -3,7 +3,7 @@ Evaluates a generated Racket program (.rkt).
|
|
3 |
"""
|
4 |
import os
|
5 |
from pathlib import Path
|
6 |
-
from safe_subprocess import run
|
7 |
from libeval import run_without_exn
|
8 |
|
9 |
|
|
|
3 |
"""
|
4 |
import os
|
5 |
from pathlib import Path
|
6 |
+
from .safe_subprocess import run
|
7 |
from libeval import run_without_exn
|
8 |
|
9 |
|
src/eval_ruby.py
CHANGED
@@ -2,7 +2,7 @@ import argparse
|
|
2 |
from sys import exit
|
3 |
import subprocess
|
4 |
from pathlib import Path
|
5 |
-
from generic_eval import main as gmain
|
6 |
|
7 |
def eval_script(path: Path):
|
8 |
try:
|
|
|
2 |
from sys import exit
|
3 |
import subprocess
|
4 |
from pathlib import Path
|
5 |
+
from .generic_eval import main as gmain
|
6 |
|
7 |
def eval_script(path: Path):
|
8 |
try:
|
src/eval_rust.py
CHANGED
@@ -2,7 +2,7 @@ import os
|
|
2 |
import subprocess
|
3 |
import tempfile
|
4 |
from pathlib import Path
|
5 |
-
from generic_eval import main
|
6 |
|
7 |
LANG_NAME = "Rust"
|
8 |
LANG_EXT = ".rs"
|
|
|
2 |
import subprocess
|
3 |
import tempfile
|
4 |
from pathlib import Path
|
5 |
+
from .generic_eval import main
|
6 |
|
7 |
LANG_NAME = "Rust"
|
8 |
LANG_EXT = ".rs"
|
src/eval_scala.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
from pathlib import Path
|
2 |
import tempfile
|
3 |
-
from safe_subprocess import run
|
4 |
|
5 |
LANG_NAME = "Scala"
|
6 |
LANG_EXT = ".scala"
|
|
|
1 |
from pathlib import Path
|
2 |
import tempfile
|
3 |
+
from .safe_subprocess import run
|
4 |
|
5 |
LANG_NAME = "Scala"
|
6 |
LANG_EXT = ".scala"
|
src/eval_sh.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
LANG_NAME = "bash"
|
5 |
LANG_EXT = ".sh"
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
LANG_NAME = "bash"
|
5 |
LANG_EXT = ".sh"
|
src/eval_swift.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import subprocess
|
2 |
from pathlib import Path
|
3 |
import os
|
4 |
-
from safe_subprocess import run
|
5 |
|
6 |
def eval_script(path: Path):
|
7 |
basename = ".".join(str(path).split(".")[:-1])
|
|
|
1 |
import subprocess
|
2 |
from pathlib import Path
|
3 |
import os
|
4 |
+
from .safe_subprocess import run
|
5 |
|
6 |
def eval_script(path: Path):
|
7 |
basename = ".".join(str(path).split(".")[:-1])
|
src/eval_ts.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
|
4 |
|
5 |
def eval_script(path: Path):
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
|
4 |
|
5 |
def eval_script(path: Path):
|
src/eval_v.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from pathlib import Path
|
2 |
-
from safe_subprocess import run
|
3 |
import subprocess
|
4 |
|
5 |
# return codes for coqc:
|
|
|
1 |
from pathlib import Path
|
2 |
+
from .safe_subprocess import run
|
3 |
import subprocess
|
4 |
|
5 |
# return codes for coqc:
|
src/libeval.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import signal
|
3 |
+
import subprocess
|
4 |
+
from typing import List
|
5 |
+
import generic_eval
|
6 |
+
|
7 |
+
def testing_mail(x, y, z):
|
8 |
+
generic_eval.gmain(x, y, z)
|
9 |
+
|
10 |
+
def run_without_exn(args: List[str]):
|
11 |
+
"""
|
12 |
+
Runs the given program with a five second timeout. Does not throw an exception
|
13 |
+
no matter what happens. The output is a dictionary of the format that we expect
|
14 |
+
for our evaluation scripts. The "status" field is "OK" when the exit code is
|
15 |
+
zero. If that isn't enough, you may want to tweak the status based on the
|
16 |
+
captured stderr and stdout.
|
17 |
+
"""
|
18 |
+
p = subprocess.Popen(
|
19 |
+
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True
|
20 |
+
)
|
21 |
+
try:
|
22 |
+
stdout, stderr = p.communicate(timeout=5)
|
23 |
+
exit_code = p.returncode
|
24 |
+
status = "OK" if exit_code == 0 else "Exception"
|
25 |
+
except subprocess.TimeoutExpired as exc:
|
26 |
+
stdout, stderr = p.stdout.read(), p.stderr.read()
|
27 |
+
os.killpg(os.getpgid(p.pid), signal.SIGTERM)
|
28 |
+
exit_code = -1
|
29 |
+
status = "Timeout"
|
30 |
+
|
31 |
+
if stdout is None:
|
32 |
+
stdout = b""
|
33 |
+
if stderr is None:
|
34 |
+
stderr = b""
|
35 |
+
return {
|
36 |
+
"status": status,
|
37 |
+
"exit_code": exit_code,
|
38 |
+
"stdout": stdout.decode("utf-8", errors="ignore"),
|
39 |
+
"stderr": stderr.decode("utf-8", errors="ignore"),
|
40 |
+
}
|
src/safe_subprocess/module_test.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from
|
2 |
import time
|
3 |
from pathlib import Path
|
4 |
|
|
|
1 |
+
from . import run
|
2 |
import time
|
3 |
from pathlib import Path
|
4 |
|
test.py
CHANGED
@@ -1,68 +1,49 @@
|
|
1 |
-
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
if
|
7 |
-
return
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
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 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import difflib
|
2 |
|
3 |
+
def remove_prefix_by_last_line(a: str, b: str, threshold: float = 0.85) -> str:
|
4 |
+
"""
|
5 |
+
基于A的最后一行定位B中的截断点,删除B中该行之前的所有内容
|
6 |
+
:param a: 前缀字符串A
|
7 |
+
:param b: 目标字符串B
|
8 |
+
:param threshold: 行相似度阈值
|
9 |
+
:return: 删除前缀后的B字符串
|
10 |
+
"""
|
11 |
+
a_lines = a.splitlines()
|
12 |
+
b_lines = b.splitlines()
|
13 |
|
14 |
+
if not a_lines:
|
15 |
+
return b
|
16 |
|
17 |
+
last_a_line = a_lines[-1]
|
18 |
+
cut_index = -1
|
19 |
|
20 |
+
for i, b_line in enumerate(b_lines):
|
21 |
+
similarity = difflib.SequenceMatcher(
|
22 |
+
None, last_a_line, b_line
|
23 |
+
).ratio()
|
24 |
+
if similarity >= threshold:
|
25 |
+
cut_index = i
|
26 |
+
break
|
27 |
|
28 |
+
if cut_index != -1:
|
29 |
+
return '\n'.join(b_lines[cut_index+1:])
|
30 |
+
else:
|
31 |
+
return b
|
32 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
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"
|
35 |
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"
|
36 |
+
# print(remove_prefix_by_last_line(A, B))
|
37 |
+
|
38 |
+
A = "#include<assert.h>\n#include<bits/stdc++.h>\n// Input to this function is a string containing multiple groups of nested parentheses. Your goal is to\n// separate those group into separate strings and return the vector of those.\n// Separate groups are balanced (each open brace is properly closed) and not nested within each other\n// Ignore any spaces in the input string.\n// >>> separate_paren_groups((\"( ) (( )) (( )( ))\"))\n// (std::vector<std::string>({(std::string)\"()\", (std::string)\"(())\", (std::string)\"(()())\"}))\nstd::vector<std::string> separate_paren_groups(std::string paren_string) {\n"
|
39 |
+
B = "#include <assert.h>\n#include <bits/stdc++.h>\n\n// Input to this function is a string containing multiple groups of nested parentheses. Your goal is to\n// separate those group into separate strings and return the vector of those.\n// Separate groups are balanced (each open brace is properly closed) and not nested within each other\n// Ignore any spaces in the input string.\n// >>> separate_paren_groups(\"(( )) (( )( ))\")\n// (std::vector<std::string>({(std::string)\"()\", (std::string)\"(())\", (std::string)\"(()())\"}))\n\nstd::vector<std::string> separate_paren_groups(std::string paren_string) {\n // Remove spaces from the input string\n paren_string.erase(std::remove(paren_string.begin(), paren_string.end(), ' '), paren_string.end());\n\n // Initialize an empty vector to store the groups\n std::vector<std::string> groups;\n\n // Initialize an empty string to build the current group\n std::string current_group;\n\n // Initialize a counter to keep track of the balance of parentheses\n int balance = 0;\n\n // Iterate over the characters in the input string\n for (char c : paren_string) {\n // If the character is an open parenthesis, increment the balance\n if (c == '(') {\n balance++;\n current_group += c;\n }\n // If the character is a close parenthesis, decrement the balance\n else if (c == ')') {\n balance--;\n current_group += c;\n // If the balance is zero, it means we've found a complete group\n if (balance == 0) {\n groups.push_back(current_group);\n current_group.clear();\n }\n }\n // If the character is not a parenthesis, ignore it\n }\n\n // Return the vector of groups\n return groups;\n}\n\nint main() {\n std::string paren_string = \"(( )) (( )( ))\";\n std::vector<std::string> groups = separate_paren_groups(paren_string);\n\n // Print the groups\n for (const auto& group : groups) {\n std::cout << group << std::endl;\n }\n\n return 0;\n}\n"
|
40 |
+
# print(remove_prefix_by_last_line(A, B))
|
41 |
+
|
42 |
+
|
43 |
+
A = "#include<assert.h>\n#include<bits/stdc++.h>\n// The Fib4 number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows:\n// fib4(0) -> 0\n// fib4(1) -> 0\n// fib4(2) -> 2\n// fib4(3) -> 0\n// fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4).\n// Please write a function to efficiently compute the n-th element of the fib4 number sequence. Do not use recursion.\n// >>> fib4((5))\n// (4)\n// >>> fib4((6))\n// (8)\n// >>> fib4((7))\n// (14)\nlong fib4(long n) {\n"
|
44 |
+
B = "#include <assert.h>\n#include <bits/stdc++.h>\n\n// The Fib4 number sequence is a sequence similar to the Fibonacci sequence that's defined as follows:\n// fib4(0) -> 0\n// fib4(1) -> 0\n// fib4(2) -> 2\n// fib4(3) -> 0\n// fib4(n) -> fib4(n-1) + fib4(n-2) + fib4(n-3) + fib4(n-4).\n// Please write a function to efficiently compute the n-th element of the fib4 number sequence. Do not use recursion.\n\n// We can use dynamic programming to solve this problem efficiently. We will store the results of subproblems in an array to avoid redundant calculations.\n\nlong fib4(long n) {\n // Base cases\n if (n <= 1) {\n return 0;\n } else if (n == 2) {\n return 2;\n } else if (n == 3) {\n return 0;\n }\n\n // Initialize an array to store the results of subproblems\n long fib4[n + 1];\n\n // Base cases\n fib4[0] = 0;\n fib4[1] = 0;\n fib4[2] = 2;\n fib4[3] = 0;\n\n // Compute the n-th element of the fib4 number sequence\n for (long i = 4; i <= n; i++) {\n fib4[i] = fib4[i - 1] + fib4[i - 2] + fib4[i - 3] + fib4[i - 4];\n }\n\n // Return the n-th element of the fib4 number sequence\n return fib4[n];\n}\n\nint main() {\n // Test the function\n std::cout << \"fib4(5) = \" << fib4(5) << std::endl;\n std::cout << \"fib4(6) = \" << fib4(6) << std::endl;\n std::cout << \"fib4(7) = \" << fib4(7) << std::endl;\n\n return 0;\n}\n"
|
45 |
+
# print(remove_prefix_by_last_line(A, B))
|
46 |
+
|
47 |
+
A = "package check_if_last_char_is_a_letter_test\n\nimport (\n \"testing\"\n \"fmt\"\n)\n\n// Create a function that returns true if the last character\n// of a given string is an alphabetical character and is not\n// a part of a word, and false otherwise.\n// Note: \"word\" is a group of characters separated by space.\n// Examples:\n// >>> check_if_last_char_is_a_letter(\"apple pie\")\n// false\n// >>> check_if_last_char_is_a_letter(\"apple pi e\")\n// true\n// >>> check_if_last_char_is_a_letter(\"apple pi e \")\n// false\n// >>> check_if_last_char_is_a_letter(\"\")\n// false\nfunc check_if_last_char_is_a_letter(txt string) bool {\n"
|
48 |
+
B = "package check_if_last_char_is_a_letter_test\n\nimport (\n \"testing\"\n \"fmt\"\n)\n\n// Create a function that returns true if the last character\n// of a given string is an alphabetical character and is not\n// a part of a word, and false otherwise.\n// Note: \"word\" is a group of characters separated by space.\n// Examples:\n// >>> check_if_last_char_is_a_letter(\"apple pie\")\n// false\n// >>> check_if_last_char_is_a_letter(\"apple pi e\")\n// true\n// >>> check_if_last_char_is_a_letter(\"apple pi e \")\n// false\n// >>> check_if_last_char_is_a_letter(\"\")\n// false\nfunc check_if_last_char_is_a_letter(txt string) bool {\n // Remove trailing spaces from the string\n txt = strings.TrimRight(txt, \" \")\n \n // Check if the string is empty\n if len(txt) == 0 {\n return false\n }\n \n // Get the last character of the string\n lastChar := txt[len(txt)-1]\n \n // Check if the last character is an alphabetical character\n if !unicode.IsLetter(rune(lastChar)) {\n return false\n }\n \n // Check if the last character is not part of a word\n if strings.Contains(txt[:len(txt)-1], \" \") {\n return true\n }\n \n return false\n}\n\nfunc TestCheckIfLastCharIsALetter(t *testing.T) {\n tests := []struct {\n input string\n want bool\n }{\n {\"apple pie\", false},\n {\"apple pi e\", true},\n {\"apple pi e \", false},\n {\"\", false},\n {\"hello world\", false},\n {\"hello world \", false},\n {\"hello world \", true},\n }\n \n for _, tt := range tests {\n got := check_if_last_char_is_a_letter(tt.input)\n if got != tt.want {\n t.Errorf(\"check_if_last_char_is_a_letter(%q) = %v, want %v\", tt.input, got, tt.want)\n }\n }\n}\n"
|
49 |
+
print(remove_prefix_by_last_line(A, B))
|