File size: 3,455 Bytes
f8a39f0 | 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | def detect_algorithm(features: dict) -> dict:
result = {
"pattern": "Unknown",
"category": "Unknown"
}
total_loops = features.get("for_loops", 0) + features.get("while_loops", 0)
# ===============================
# Dynamic Programming
# ===============================
if features.get("memoization_pattern"):
return {"pattern": "Memoization", "category": "Dynamic Programming"}
if features.get("tabulation_pattern"):
return {"pattern": "Tabulation", "category": "Dynamic Programming"}
# ===============================
# Heap
# ===============================
if features.get("heap_pattern"):
return {"pattern": "Heap-Based Algorithm", "category": "Data Structure Based"}
# ===============================
# Search
# ===============================
if features.get("binary_search_pattern"):
return {"pattern": "Binary Search", "category": "Search Algorithm"}
# ===============================
# Graph
# ===============================
if features.get("bfs_pattern"):
return {"pattern": "Breadth-First Search", "category": "Graph Algorithm"}
if features.get("dfs_pattern"):
return {"pattern": "Depth-First Search", "category": "Graph Algorithm"}
# ===============================
# Pointer Techniques
# ===============================
if features.get("sliding_window_pattern"):
return {"pattern": "Sliding Window", "category": "Pointer Technique"}
if (
len(features.get("pointer_variables", [])) >= 2
and features.get("pointer_updates", 0) >= 2
and features.get("while_loops", 0) >= 1
):
return {"pattern": "Two-Pointer Technique", "category": "Pointer Technique"}
# ===============================
# Sorting Algorithms
# ===============================
if features.get("bubble_sort_pattern"):
return {"pattern": "Bubble Sort", "category": "Sorting Algorithm"}
if features.get("insertion_sort_pattern"):
return {"pattern": "Insertion Sort", "category": "Sorting Algorithm"}
if features.get("merge_sort_pattern"):
return {"pattern": "Merge Sort", "category": "Sorting Algorithm"}
if features.get("quick_sort_pattern"):
return {"pattern": "Quick Sort", "category": "Sorting Algorithm"}
# ===============================
# Recursive Patterns
# ===============================
if features.get("divide_and_conquer"):
return {"pattern": "Recursive Divide-and-Conquer", "category": "Divide-and-Conquer"}
if features.get("recursion") and features.get("recursive_call_count", 0) >= 2:
return {"pattern": "Recursive (Exponential)", "category": "Recursive Pattern"}
if features.get("recursion"):
return {"pattern": "Recursive (Linear)", "category": "Recursive Pattern"}
# ===============================
# Iterative Patterns
# ===============================
if features.get("max_loop_depth", 0) >= 2:
return {"pattern": "Nested Iterative", "category": "Iterative Pattern"}
if total_loops == 1:
return {"pattern": "Linear Iterative", "category": "Iterative Pattern"}
if total_loops == 0:
return {"pattern": "Constant-Time", "category": "Direct Computation"}
return result |