Skip to content

Commit eee00b4

Browse files
committed
fix: Handle integer output parsing in JUDGE_FUNC for 7 solutions
The test framework uses ast.literal_eval() to parse solution outputs, which converts single numbers like "1" to integers. The judge functions in 7 solutions were only checking for str or list types, causing test failures when outputs were parsed as integers. Fixed solutions: - 0021_merge_two_sorted_lists: Handle int input + fix empty line parsing - 0027_remove_element: Handle int input for k=0 single-line output case - 0075_sort_colors: Handle int input for single-element arrays - 0088_merge_sorted_array: Handle int input + fix empty array parsing - 0283_move_zeroes: Handle int input for single-element arrays - 0876_middle_of_the_linked_list: Handle int input for single-node lists - 0905_sort_array_by_parity: Handle int input for single-element arrays Changes: - Added isinstance(actual, int) checks in all judge functions - Convert single integers to single-element lists: [actual] - Improved input parsing to preserve empty lines correctly - No changes to solution algorithms (all were already correct) Test Results: - All 92 tests pass (7 skipped for problems without static tests) - Static tests: 33/33 passing - Generated tests: 59/59 passing - Combined tests: 99/99 passing Root Cause: The compare.py module uses ast.literal_eval() to parse outputs before passing to JUDGE_FUNC. Single numbers get parsed as int, not str, which the judge functions didn't handle.
1 parent 5b443a2 commit eee00b4

File tree

7 files changed

+57
-30
lines changed

7 files changed

+57
-30
lines changed

solutions/0021_merge_two_sorted_lists.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,29 @@ def judge(actual, expected, input_data: str) -> bool:
9191
Validate result: check if actual output is correctly merged sorted list.
9292
9393
Args:
94-
actual: Program output (space-separated integers as string)
94+
actual: Program output (space-separated integers as string, list, or single int)
9595
expected: Expected output (None if from generator)
9696
input_data: Raw input string (Line 1: list1, Line 2: list2)
9797
9898
Returns:
9999
bool: True if correctly merged
100100
"""
101-
lines = input_data.strip().split('\n')
101+
# Parse input - preserve empty lines by splitting before strip
102+
lines = input_data.split('\n')
103+
# Handle trailing newline by removing empty last element if present
104+
if lines and lines[-1] == '':
105+
lines = lines[:-1]
106+
102107
list1_vals = list(map(int, lines[0].split())) if lines[0].strip() else []
103108
list2_vals = list(map(int, lines[1].split())) if len(lines) > 1 and lines[1].strip() else []
104109

105110
# Compute correct answer
106111
correct = sorted(list1_vals + list2_vals)
107112

108-
# Parse actual output
109-
if isinstance(actual, str):
113+
# Parse actual output - handle int (from ast.literal_eval), str, or list
114+
if isinstance(actual, int):
115+
actual_vals = [actual]
116+
elif isinstance(actual, str):
110117
actual_vals = list(map(int, actual.strip().split())) if actual.strip() else []
111118
elif isinstance(actual, list):
112119
actual_vals = actual

solutions/0027_remove_element.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def judge(actual, expected, input_data: str) -> bool:
7979
Validate result: check if actual output correctly removes all occurrences of val.
8080
8181
Args:
82-
actual: Program output (may be string with newlines or tuple)
82+
actual: Program output (may be string with newlines, tuple, or int when k=0)
8383
expected: Expected output (None if from generator)
8484
input_data: Raw input string (Line 1: nums, Line 2: val)
8585
@@ -90,14 +90,19 @@ def judge(actual, expected, input_data: str) -> bool:
9090
nums = list(map(int, lines[0].split())) if lines[0] else []
9191
val = int(lines[1]) if len(lines) > 1 else 0
9292

93-
# Parse actual output
94-
if isinstance(actual, str):
93+
# Parse actual output - handle various formats
94+
if isinstance(actual, int):
95+
# ast.literal_eval parsed single number (k=0 case, no second line)
96+
k = actual
97+
result_nums = []
98+
elif isinstance(actual, str):
9599
lines_out = actual.strip().split('\n')
96-
if len(lines_out) >= 2:
97-
k = int(lines_out[0])
98-
result_nums = list(map(int, lines_out[1].split())) if lines_out[1] else []
100+
k = int(lines_out[0])
101+
if len(lines_out) >= 2 and lines_out[1]:
102+
result_nums = list(map(int, lines_out[1].split()))
99103
else:
100-
return False
104+
# k=0 case: no elements remaining
105+
result_nums = []
101106
elif isinstance(actual, tuple) and len(actual) == 2:
102107
k, result_nums = actual
103108
else:

solutions/0075_sort_colors.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def judge(actual, expected, input_data: str) -> bool:
8484
Validate result: check if actual output is correctly sorted (0s, 1s, 2s).
8585
8686
Args:
87-
actual: Program output (space-separated integers as string)
87+
actual: Program output (space-separated integers as string, list, or single int)
8888
expected: Expected output (None if from generator)
8989
input_data: Raw input string (space-separated integers 0, 1, or 2)
9090
@@ -94,8 +94,10 @@ def judge(actual, expected, input_data: str) -> bool:
9494
line = input_data.strip()
9595
nums = list(map(int, line.split())) if line else []
9696

97-
# Parse actual output
98-
if isinstance(actual, str):
97+
# Parse actual output - handle int (from ast.literal_eval), str, or list
98+
if isinstance(actual, int):
99+
actual_nums = [actual]
100+
elif isinstance(actual, str):
99101
actual_nums = list(map(int, actual.strip().split())) if actual.strip() else []
100102
elif isinstance(actual, list):
101103
actual_nums = actual

solutions/0088_merge_sorted_array.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,34 @@ def judge(actual, expected, input_data: str) -> bool:
9090
Validate result: check if actual output is correctly merged sorted array.
9191
9292
Args:
93-
actual: Program output (space-separated integers as string)
93+
actual: Program output (space-separated integers as string, list, or single int)
9494
expected: Expected output (None if from generator)
9595
input_data: Raw input string (Line 1: nums1, Line 2: m, Line 3: nums2, Line 4: n)
9696
9797
Returns:
9898
bool: True if correctly merged
9999
"""
100-
lines = input_data.strip().split('\n')
101-
nums1 = list(map(int, lines[0].split())) if lines[0] else []
102-
m = int(lines[1]) if len(lines) > 1 else 0
100+
# Parse input - preserve empty lines by splitting before strip
101+
lines = input_data.split('\n')
102+
# Handle trailing newline by removing empty last element if present
103+
while lines and lines[-1] == '':
104+
lines.pop()
105+
106+
nums1 = list(map(int, lines[0].split())) if lines[0].strip() else []
107+
m = int(lines[1]) if len(lines) > 1 and lines[1].strip() else 0
103108
nums2 = list(map(int, lines[2].split())) if len(lines) > 2 and lines[2].strip() else []
104-
n = int(lines[3]) if len(lines) > 3 else 0
109+
n = int(lines[3]) if len(lines) > 3 and lines[3].strip() else 0
105110

106111
# Extract actual elements from nums1 (first m elements)
107112
nums1_actual = nums1[:m] if m > 0 else []
108113

109114
# Compute correct answer
110115
correct = sorted(nums1_actual + nums2)
111116

112-
# Parse actual output
113-
if isinstance(actual, str):
117+
# Parse actual output - handle int (from ast.literal_eval), str, or list
118+
if isinstance(actual, int):
119+
actual_vals = [actual]
120+
elif isinstance(actual, str):
114121
actual_vals = list(map(int, actual.strip().split())) if actual.strip() else []
115122
elif isinstance(actual, list):
116123
actual_vals = actual

solutions/0283_move_zeroes.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def judge(actual, expected, input_data: str) -> bool:
9191
Validate result: check if actual output has all zeros moved to end.
9292
9393
Args:
94-
actual: Program output (space-separated integers as string)
94+
actual: Program output (space-separated integers as string, list, or single int)
9595
expected: Expected output (None if from generator)
9696
input_data: Raw input string (space-separated integers)
9797
@@ -101,8 +101,10 @@ def judge(actual, expected, input_data: str) -> bool:
101101
line = input_data.strip()
102102
nums = list(map(int, line.split())) if line else []
103103

104-
# Parse actual output
105-
if isinstance(actual, str):
104+
# Parse actual output - handle int (from ast.literal_eval), str, or list
105+
if isinstance(actual, int):
106+
actual_nums = [actual]
107+
elif isinstance(actual, str):
106108
actual_nums = list(map(int, actual.strip().split())) if actual.strip() else []
107109
elif isinstance(actual, list):
108110
actual_nums = actual

solutions/0876_middle_of_the_linked_list.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def judge(actual, expected, input_data: str) -> bool:
8989
Validate result: check if actual output is the middle node values.
9090
9191
Args:
92-
actual: Program output (space-separated integers as string)
92+
actual: Program output (space-separated integers as string, list, or single int)
9393
expected: Expected output (None if from generator)
9494
input_data: Raw input string (space-separated node values)
9595
@@ -106,8 +106,10 @@ def judge(actual, expected, input_data: str) -> bool:
106106
mid_idx = len(values) // 2
107107
correct = values[mid_idx:]
108108

109-
# Parse actual output
110-
if isinstance(actual, str):
109+
# Parse actual output - handle int (from ast.literal_eval), str, or list
110+
if isinstance(actual, int):
111+
actual_vals = [actual]
112+
elif isinstance(actual, str):
111113
actual_vals = list(map(int, actual.strip().split())) if actual.strip() else []
112114
elif isinstance(actual, list):
113115
actual_vals = actual

solutions/0905_sort_array_by_parity.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def judge(actual, expected, input_data: str) -> bool:
7979
Validate result: check if actual output has evens before odds.
8080
8181
Args:
82-
actual: Program output (space-separated integers as string)
82+
actual: Program output (space-separated integers as string, list, or single int)
8383
expected: Expected output (None if from generator)
8484
input_data: Raw input string (space-separated integers)
8585
@@ -89,8 +89,10 @@ def judge(actual, expected, input_data: str) -> bool:
8989
line = input_data.strip()
9090
nums = list(map(int, line.split())) if line else []
9191

92-
# Parse actual output
93-
if isinstance(actual, str):
92+
# Parse actual output - handle int (from ast.literal_eval), str, or list
93+
if isinstance(actual, int):
94+
actual_nums = [actual]
95+
elif isinstance(actual, str):
9496
actual_nums = list(map(int, actual.strip().split())) if actual.strip() else []
9597
elif isinstance(actual, list):
9698
actual_nums = actual

0 commit comments

Comments
 (0)