Skip to content

Commit 7040f88

Browse files
committed
fix utils bug
1 parent ec10aa2 commit 7040f88

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

leetcode_local_tester/creator/python3_creator.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ def _create_func_problem_test(self, dir_loc, p: Problem):
7474
tmp = input_param.split(": ")
7575
input_type = tmp[1]
7676
# remove Optional
77+
while "Optional[" in input_type:
78+
beg_idx = input_type.find("Optional[")
79+
score = 0
80+
end_idx = -1
81+
for i in range(beg_idx, len(input_type)):
82+
if input_type[i] == "[":
83+
score += 1
84+
elif input_type[i] == "]":
85+
score -= 1
86+
if score == 0:
87+
end_idx = i
88+
break
89+
input_type = input_type[:beg_idx] + input_type[beg_idx + 9:end_idx] + input_type[end_idx + 1:]
90+
7791
if input_type.startswith("Optional["):
7892
input_type = input_type[9:-1]
7993
input_name = tmp[0]
@@ -82,8 +96,21 @@ def _create_func_problem_test(self, dir_loc, p: Problem):
8296
f"{input_name}: {input_type} = convert_params(data[i * one_test_number + {idx}], '{input_type}')")
8397
input_names.append(input_name)
8498

85-
if f.output_params.startswith("Optional["):
86-
f.output_params = f.output_params[9:-1]
99+
while "Optional[" in f.output_params:
100+
beg_idx = f.output_params.find("Optional[")
101+
score = 0
102+
end_idx = -1
103+
for i in range(beg_idx, len(f.output_params)):
104+
if f.output_params[i] == "[":
105+
score += 1
106+
elif f.output_params[i] == "]":
107+
score -= 1
108+
if score == 0:
109+
end_idx = i
110+
break
111+
f.output_params = f.output_params[:beg_idx] + f.output_params[beg_idx + 9:end_idx] + f.output_params[
112+
end_idx + 1:]
113+
87114
build_params_str_list.append(
88115
f"real_res: {f.output_params} = convert_params(data[i * one_test_number + {input_number}], '{f.output_params}')")
89116
build_params_str_list = [self._generate_code_with_indent(s, 2) for s in build_params_str_list]

leetcode_local_tester/template/utils/cpp/help.hpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,14 @@ bool compare_result(string sample_idx, TreeNode *my_ans, TreeNode *result) {
216216
};
217217

218218
bool compare_result(string sample_idx, ListNode *my_ans, ListNode *result) {
219-
vector<long long> a = my_ans->to_vector();
220-
vector<long long> b = result->to_vector();
219+
vector<long long> a, b;
220+
// https://leetcode.cn/problems/merge-k-sorted-lists/
221+
if (my_ans != NULL) {
222+
a = my_ans->to_vector();
223+
}
224+
if (result != NULL) {
225+
b = result->to_vector();
226+
}
221227
return compare_result(sample_idx, a, b);
222228
}
223229

@@ -327,7 +333,12 @@ void convert_params(string str, vector<T> &v) {
327333
void convert_params(string str, ListNode *&res) {
328334
vector<long long> v;
329335
convert_params(str, v);
330-
res = new ListNode(v);
336+
// https://leetcode.cn/problems/merge-k-sorted-lists/
337+
if (v.size() == 0) {
338+
res = NULL;
339+
} else {
340+
res = new ListNode(v);
341+
}
331342
}
332343

333344
vector<string> __split(string &s, string delimiter) {

0 commit comments

Comments
 (0)