Skip to content

Commit 21a3c66

Browse files
authored
Merge pull request #36 from tarone-saloni/Dark
Add Algorithm Learning Mode with Tutorials frontend
2 parents c38b3fc + 44c46e7 commit 21a3c66

File tree

15 files changed

+1009
-1
lines changed

15 files changed

+1009
-1
lines changed

backend/main.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import os
22
from pathlib import Path
3-
from fastapi import FastAPI, HTTPException
3+
from fastapi import FastAPI, HTTPException, Query
44
from fastapi.middleware.cors import CORSMiddleware
55
from fastapi.staticfiles import StaticFiles
66
from fastapi.responses import FileResponse
77
from pydantic import BaseModel, ConfigDict
88
from typing import List, Optional, Dict, Any
9+
from services.tutorial_service import TutorialService
10+
from models.tutorial_models import Tutorial
911
import uvicorn
1012

1113
app = FastAPI(
@@ -14,6 +16,31 @@
1416
version="1.0.0"
1517
)
1618

19+
# Tutorial routes
20+
@app.get("/api/tutorials", response_model=List[Tutorial])
21+
async def get_tutorials(
22+
difficulty: Optional[str] = Query(None, description="Filter tutorials by difficulty level"),
23+
category: Optional[str] = Query(None, description="Filter tutorials by category")
24+
):
25+
"""
26+
Get all tutorials with optional filtering by difficulty or category.
27+
"""
28+
if difficulty:
29+
return TutorialService.get_tutorials_by_difficulty(difficulty)
30+
elif category:
31+
return TutorialService.get_tutorials_by_category(category)
32+
return TutorialService.get_all_tutorials()
33+
34+
@app.get("/api/tutorials/{tutorial_id}", response_model=Tutorial)
35+
async def get_tutorial(tutorial_id: str):
36+
"""
37+
Get a specific tutorial by ID.
38+
"""
39+
tutorial = TutorialService.get_tutorial_by_id(tutorial_id)
40+
if not tutorial:
41+
raise HTTPException(status_code=404, detail="Tutorial not found")
42+
return tutorial
43+
1744
# Environment detection
1845
ENVIRONMENT = os.getenv("ENVIRONMENT", "development")
1946
IS_PRODUCTION = ENVIRONMENT == "production"

backend/models/tutorial_models.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List, Optional
2+
from pydantic import BaseModel
3+
4+
class QuizOption(BaseModel):
5+
text: str
6+
isCorrect: bool
7+
8+
class Quiz(BaseModel):
9+
question: str
10+
options: List[QuizOption]
11+
explanation: Optional[str] = None
12+
13+
class TutorialStep(BaseModel):
14+
id: str
15+
type: str # 'explanation', 'quiz', 'interactive'
16+
title: str
17+
content: str
18+
quiz: Optional[Quiz] = None
19+
codeExample: Optional[str] = None
20+
interactive: bool = False
21+
22+
class Tutorial(BaseModel):
23+
id: str
24+
title: str
25+
description: str
26+
difficulty: str # 'Beginner', 'Intermediate', 'Advanced'
27+
estimatedTime: str
28+
prerequisites: List[str]
29+
steps: List[TutorialStep]
30+
category: str # 'sorting', 'graph', 'string', 'dp'
31+
icon: str
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from typing import Dict, List, Optional
2+
from models.tutorial_models import Tutorial, TutorialStep, Quiz, QuizOption
3+
4+
# Mock database of tutorials
5+
TUTORIALS_DB: Dict[str, Tutorial] = {
6+
"sorting": Tutorial(
7+
id="sorting",
8+
title="Sorting Algorithms Mastery",
9+
description="Learn about different sorting algorithms and their implementations",
10+
difficulty="Beginner",
11+
estimatedTime="30 minutes",
12+
prerequisites=["basic-arrays"],
13+
category="sorting",
14+
icon="🔄",
15+
steps=[
16+
TutorialStep(
17+
id="intro",
18+
type="explanation",
19+
title="Introduction to Sorting",
20+
content="Sorting is a fundamental operation in computer science...",
21+
interactive=True
22+
),
23+
TutorialStep(
24+
id="bubble-sort",
25+
type="explanation",
26+
title="Bubble Sort Algorithm",
27+
content="Bubble Sort is the simplest sorting algorithm...",
28+
codeExample="""def bubble_sort(arr):
29+
n = len(arr)
30+
for i in range(n):
31+
for j in range(0, n-i-1):
32+
if arr[j] > arr[j+1]:
33+
arr[j], arr[j+1] = arr[j+1], arr[j]
34+
return arr""",
35+
interactive=True,
36+
quiz=Quiz(
37+
question="What is the time complexity of Bubble Sort?",
38+
options=[
39+
QuizOption(text="O(n)", isCorrect=False),
40+
QuizOption(text="O(n²)", isCorrect=True),
41+
QuizOption(text="O(n log n)", isCorrect=False),
42+
QuizOption(text="O(1)", isCorrect=False)
43+
],
44+
explanation="Bubble Sort uses nested loops, leading to O(n²) time complexity"
45+
)
46+
)
47+
]
48+
),
49+
"graph": Tutorial(
50+
id="graph",
51+
title="Graph Algorithms",
52+
description="Master graph traversal and pathfinding algorithms",
53+
difficulty="Intermediate",
54+
estimatedTime="45 minutes",
55+
prerequisites=["basic-arrays", "sorting"],
56+
category="graph",
57+
icon="🕸️",
58+
steps=[
59+
TutorialStep(
60+
id="intro",
61+
type="explanation",
62+
title="Introduction to Graphs",
63+
content="Graphs are versatile data structures...",
64+
interactive=True
65+
)
66+
]
67+
)
68+
}
69+
70+
class TutorialService:
71+
@staticmethod
72+
def get_all_tutorials() -> List[Tutorial]:
73+
"""Get all available tutorials."""
74+
return list(TUTORIALS_DB.values())
75+
76+
@staticmethod
77+
def get_tutorial_by_id(tutorial_id: str) -> Optional[Tutorial]:
78+
"""Get a specific tutorial by ID."""
79+
return TUTORIALS_DB.get(tutorial_id)
80+
81+
@staticmethod
82+
def get_tutorials_by_difficulty(difficulty: str) -> List[Tutorial]:
83+
"""Get tutorials filtered by difficulty level."""
84+
return [
85+
tutorial for tutorial in TUTORIALS_DB.values()
86+
if tutorial.difficulty.lower() == difficulty.lower()
87+
]
88+
89+
@staticmethod
90+
def get_tutorials_by_category(category: str) -> List[Tutorial]:
91+
"""Get tutorials filtered by category."""
92+
return [
93+
tutorial for tutorial in TUTORIALS_DB.values()
94+
if tutorial.category.lower() == category.lower()
95+
]

0 commit comments

Comments
 (0)