1515
1616from .parser .parser import PseudocodeParser
1717from .analyzer .complexity_analyzer import ComplexityAnalyzer , AnalysisResult
18+ from .analyzer .feedback_generator import FeedbackGenerator , FeedbackLevel
1819from .schemas .complexity import ComplexityClass
1920
2021
@@ -257,10 +258,21 @@ def _generate_feedback(
257258 is_correct : bool ,
258259 eval_options : Dict
259260) -> str :
260- """Generate comprehensive feedback for the student."""
261+ """Generate comprehensive feedback for the student using FeedbackGenerator."""
262+ # Use FeedbackGenerator for the core analysis feedback
263+ feedback_generator = FeedbackGenerator ()
264+
265+ # Determine feedback level based on options
266+ show_detailed = eval_options .get ('show_detailed_feedback' , True )
267+ level = FeedbackLevel .DETAILED if show_detailed and not is_correct else FeedbackLevel .STANDARD
268+
269+ # Generate detailed feedback from analysis
270+ detailed_feedback = feedback_generator .generate (analysis , level )
271+
272+ # Build the final feedback string
261273 lines = []
262274
263- # Overall result
275+ # Overall result header
264276 if is_correct :
265277 lines .append ("✓ Correct! Your algorithm meets the complexity requirements." )
266278 else :
@@ -275,11 +287,11 @@ def _generate_feedback(
275287
276288 if time_correct :
277289 if time_result ['comparison' ] < 0 :
278- lines .append (f " ✓ Excellent! Your algorithm is more efficient than required." )
290+ lines .append (" ✓ Excellent! Your algorithm is more efficient than required." )
279291 else :
280- lines .append (f " ✓ Your algorithm meets the time complexity requirement." )
292+ lines .append (" ✓ Your algorithm meets the time complexity requirement." )
281293 else :
282- lines .append (f " ✗ Your algorithm exceeds the allowed time complexity." )
294+ lines .append (" ✗ Your algorithm exceeds the allowed time complexity." )
283295 lines .append (f" Try to optimize your algorithm to achieve { time_result ['expected' ].value } ." )
284296
285297 # Student's stated complexity feedback
@@ -299,38 +311,26 @@ def _generate_feedback(
299311 lines .append (f" • Detected: { space_result ['detected' ].value } " )
300312
301313 if space_correct :
302- lines .append (f " ✓ Your algorithm meets the space complexity requirement." )
314+ lines .append (" ✓ Your algorithm meets the space complexity requirement." )
303315 else :
304- lines .append (f " ✗ Your algorithm exceeds the allowed space complexity." )
316+ lines .append (" ✗ Your algorithm exceeds the allowed space complexity." )
305317 lines .append ("" )
306318
307- # Detailed analysis (if enabled)
308- if eval_options . get ( 'show_detailed_feedback' , True ) and not is_correct :
319+ # Add detailed analysis from FeedbackGenerator (if enabled and incorrect )
320+ if show_detailed and not is_correct :
309321 lines .append ("-" * 50 )
310- lines .append ("Analysis Details:" )
311322
312- if analysis .loops :
313- lines .append (f" • Found { len (analysis .loops )} loop(s)" )
314- if analysis .max_nesting_depth > 1 :
315- lines .append (f" • Maximum nesting depth: { analysis .max_nesting_depth } " )
323+ # Add sections from FeedbackGenerator
324+ for section in detailed_feedback .sections :
325+ lines .append (f"[{ section .importance .upper ()} ] { section .title } " )
326+ lines .append (section .content )
327+ lines .append ("" )
316328
317- if analysis .recursion :
318- rec = analysis .recursion
319- lines .append (f" • Recursive function: { rec .function_name } ()" )
320- lines .append (f" • Recurrence: { rec .recurrence } " )
321-
322- # Suggestions
323- lines .append ("" )
324- lines .append ("Suggestions:" )
325- if time_result ['detected' ] == ComplexityClass .QUADRATIC and time_result ['expected' ] == ComplexityClass .LINEAR :
326- lines .append (" • Consider if you can eliminate one of the nested loops" )
327- lines .append (" • Using a hash table/dictionary might help reduce complexity" )
328- elif time_result ['detected' ] == ComplexityClass .EXPONENTIAL :
329- lines .append (" • Consider using dynamic programming or memoization" )
330- lines .append (" • Avoid redundant recursive calls by caching results" )
331- elif time_result ['detected' ] == ComplexityClass .CUBIC :
332- lines .append (" • Triple nested loops are often avoidable" )
333- lines .append (" • Consider algorithmic improvements like divide-and-conquer" )
329+ # Add suggestions from FeedbackGenerator
330+ if detailed_feedback .suggestions :
331+ lines .append ("Suggestions:" )
332+ for suggestion in detailed_feedback .suggestions :
333+ lines .append (f" • { suggestion } " )
334334
335335 return "\n " .join (lines )
336336
@@ -353,60 +353,3 @@ def _format_parse_error(parse_result) -> str:
353353 return "\n " .join (lines )
354354
355355
356- def _build_feedback_items (
357- time_result : Dict ,
358- space_result : Optional [Dict ],
359- is_correct : bool ,
360- score : float
361- ) -> list :
362- """
363- Build feedback items as list of (level, message) tuples.
364-
365- Levels: 'success', 'warning', 'error', 'info'
366- """
367- from typing import List , Tuple
368- items : List [Tuple [str , str ]] = []
369-
370- # Overall result
371- if is_correct :
372- items .append (("success" , "Your algorithm meets the complexity requirements." ))
373- else :
374- items .append (("error" , "Your algorithm does not meet the complexity requirements." ))
375-
376- # Time complexity feedback
377- time_correct = time_result ['is_correct' ]
378- detected = time_result ['detected' ].value
379- expected = time_result ['expected' ].value
380-
381- if time_correct :
382- if time_result ['comparison' ] < 0 :
383- items .append (("success" , f"Time complexity: { detected } (better than required { expected } )" ))
384- else :
385- items .append (("success" , f"Time complexity: { detected } (meets requirement)" ))
386- else :
387- items .append (("error" , f"Time complexity: { detected } (exceeds required { expected } )" ))
388- items .append (("info" , f"Try to optimize your algorithm to achieve { expected } or better." ))
389-
390- # Student's stated complexity feedback
391- if time_result .get ('student_stated' ):
392- if time_result .get ('student_stated_correct' ):
393- items .append (("success" , f"Your stated complexity ({ time_result ['student_stated' ]} ) matches the detected complexity." ))
394- else :
395- items .append (("warning" , f"Your stated complexity ({ time_result ['student_stated' ]} ) differs from detected ({ detected } )." ))
396-
397- # Space complexity feedback (if applicable)
398- if space_result :
399- space_correct = space_result ['is_correct' ]
400- space_detected = space_result ['detected' ].value
401- space_expected = space_result ['expected' ].value
402-
403- if space_correct :
404- items .append (("success" , f"Space complexity: { space_detected } (meets requirement)" ))
405- else :
406- items .append (("error" , f"Space complexity: { space_detected } (exceeds required { space_expected } )" ))
407-
408- # Score info
409- if not is_correct and score > 0 :
410- items .append (("info" , f"Partial credit: { score :.0%} " ))
411-
412- return items
0 commit comments