Spaces:
Build error
Build error
File size: 1,862 Bytes
c29f1fd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | """Analyzer for DSA and competitive-programming style Python code."""
from __future__ import annotations
from typing import Any, Dict
from schemas.response import AnalysisIssue, DomainAnalysis
def analyze_dsa_code(code: str, parsed: Dict[str, Any], complexity: Dict[str, Any]) -> DomainAnalysis:
"""Inspect algorithmic code for brute-force patterns and efficiency risks."""
issues = []
suggestions = []
score = 0.7
if parsed.get("max_loop_depth", 0) >= 2:
issues.append(
AnalysisIssue(
title="Nested loops suggest brute-force behavior",
severity="medium",
description="The implementation scans the input multiple times, which is often avoidable in DSA problems.",
)
)
suggestions.append("Consider replacing nested scans with a hashmap, prefix table, or sorted search strategy.")
score -= 0.15
if parsed.get("uses_recursion"):
suggestions.append("Verify recursion depth and add memoization or iterative conversion if the input size can grow.")
score -= 0.05
if "sorted(" in code or ".sort(" in code:
suggestions.append("Sorting is acceptable here, but validate whether a direct O(n) pass can remove the sort.")
if not suggestions:
suggestions.append("Document the intended time complexity and add edge-case checks for empty input and duplicates.")
return DomainAnalysis(
domain="dsa",
domain_score=max(0.05, round(score, 4)),
issues=issues,
suggestions=suggestions,
highlights={
"time_complexity": complexity["time_complexity"],
"space_complexity": complexity["space_complexity"],
"max_loop_depth": float(parsed.get("max_loop_depth", 0)),
},
)
|