Skip to content

Commit 16a2298

Browse files
Changed behaviour so that the command handler finished as soon as a match is found
1 parent 202d513 commit 16a2298

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

tools/commands.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ def get_case_feedback(
170170
issues encountered when evaluating each case against the student's
171171
response.
172172
"""
173-
matches, feedback, warnings = evaluate_all_cases(response, params, cases)
173+
# NOTE: Previous behaviour, where all cases are evaluated but only the feedback
174+
# for the first case is returned can be restored by changing the line below to:
175+
# matches, feedback, warnings = evaluate_all_cases(response, params, cases)
176+
matches, feedback, warnings = find_first_matching_case(response, params, cases)
174177

175178
if not matches:
176179
return None, warnings
@@ -198,6 +201,38 @@ def get_case_feedback(
198201

199202
return match, warnings
200203

204+
def find_first_matching_case(
205+
response: Any,
206+
params: Dict,
207+
cases: List[Dict],
208+
) -> Tuple[List[int], List[str], List[CaseWarning]]:
209+
"""Evaluates cases until it finds matching one.
210+
211+
Args:
212+
response (Any): The student's response.
213+
params (Dict): The params of the evaluation function.
214+
cases (List[Dict]): A list of cases to check against.
215+
216+
Returns:
217+
Tuple[List[int], List[str], List[CaseWarning]]: Returns a list of
218+
indices of cases that match a student's response, a list of feedback
219+
strings from each case, and a list of issues encountered when
220+
evaluating cases against the student's response.
221+
"""
222+
matches, feedback, warnings = [], [], []
223+
224+
for index, case in enumerate(cases):
225+
result = evaluate_case(response, params, case, index)
226+
227+
if result.warning is not None:
228+
warnings.append(result.warning)
229+
230+
if result.is_correct:
231+
matches.append(index)
232+
feedback.append(result.feedback)
233+
break
234+
235+
return matches, feedback, warnings
201236

202237
def evaluate_all_cases(
203238
response: Any,

0 commit comments

Comments
 (0)