'use client' import React, { useState, useEffect } from 'react' import { useRouter, useParams } from 'next/navigation' import { Play, Clock, CheckCircle, XCircle, ArrowLeft, Trophy } from 'lucide-react' interface TestCase { input: string expected: string description: string } interface Problem { id: string title: string description: string difficulty: 'Easy' | 'Medium' | 'Hard' category: string examples: TestCase[] constraints: string[] hints: string[] starter_code: string function_name: string } export default function ProblemPage() { const params = useParams() const router = useRouter() const problemId = params.problemId as string const [problem, setProblem] = useState(null) const [code, setCode] = useState('') const [output, setOutput] = useState('') const [testResults, setTestResults] = useState([]) const [isRunning, setIsRunning] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false) const [showHints, setShowHints] = useState(false) const [activeTab, setActiveTab] = useState<'description' | 'examples' | 'constraints'>('description') useEffect(() => { loadProblem(problemId) }, [problemId]) const loadProblem = async (id: string) => { try { // In a real app, this would fetch from your backend const problems: Record = { 'string-capitalizer': { id: 'string-capitalizer', title: 'String Capitalizer', description: 'Write a function that takes a string as input and returns the string converted to uppercase.', difficulty: 'Easy', category: 'String Manipulation', examples: [ { input: 'hello', expected: 'HELLO', description: 'Basic string conversion' }, { input: 'world', expected: 'WORLD', description: 'Another basic case' }, { input: 'Python Programming', expected: 'PYTHON PROGRAMMING', description: 'String with spaces' } ], constraints: [ 'Input string length will be between 1 and 1000 characters', 'Input may contain letters, numbers, and spaces', 'Function must be named exactly "capitalize_string"' ], hints: [ 'Python has a built-in method to convert strings to uppercase', 'The upper() method can be used on any string', 'Remember to return the result, not just print it' ], starter_code: 'def capitalize_string(text):\n # Write your solution here\n pass', function_name: 'capitalize_string' }, 'reverse-string': { id: 'reverse-string', title: 'Reverse String', description: 'Write a function that takes a string and returns it reversed.', difficulty: 'Easy', category: 'String Manipulation', examples: [ { input: 'hello', expected: 'olleh', description: 'Basic string reversal' }, { input: 'python', expected: 'nohtyp', description: 'Another basic case' }, { input: 'OpenLearnX', expected: 'XnraeLnepO', description: 'Mixed case string' } ], constraints: [ 'Input string length will be between 1 and 1000 characters', 'Function must be named exactly "reverse_string"' ], hints: [ 'Python strings can be sliced with [::-1]', 'You can also use the reversed() function', 'Remember to return the result' ], starter_code: 'def reverse_string(text):\n # Write your solution here\n pass', function_name: 'reverse_string' }, 'fibonacci': { id: 'fibonacci', title: 'Fibonacci Sequence', description: 'Write a function that returns the nth number in the Fibonacci sequence.', difficulty: 'Medium', category: 'Algorithms', examples: [ { input: '0', expected: '0', description: 'First Fibonacci number' }, { input: '1', expected: '1', description: 'Second Fibonacci number' }, { input: '5', expected: '5', description: 'Sixth Fibonacci number (0,1,1,2,3,5)' } ], constraints: [ 'n will be between 0 and 30', 'Function must be named exactly "fibonacci"', 'Should handle edge cases for n=0 and n=1' ], hints: [ 'Base cases: fib(0) = 0, fib(1) = 1', 'For n > 1: fib(n) = fib(n-1) + fib(n-2)', 'Consider using iteration instead of recursion for better performance' ], starter_code: 'def fibonacci(n):\n # Write your solution here\n pass', function_name: 'fibonacci' } } const selectedProblem = problems[id] if (selectedProblem) { setProblem(selectedProblem) setCode(selectedProblem.starter_code) } else { // Problem not found router.push('/coding') } } catch (error) { console.error('Failed to load problem:', error) router.push('/coding') } } const runCode = async () => { if (!problem || !code.trim()) return setIsRunning(true) setOutput('') setTestResults([]) try { const response = await fetch('http://127.0.0.1:5000/api/coding/execute', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ code, language: 'python', problem_id: problem.id, test_cases: problem.examples }) }) const result = await response.json() if (result.success) { setOutput(result.output || 'Code executed successfully') setTestResults(result.test_results || []) } else { setOutput(`Error: ${result.error}`) } } catch (error) { setOutput(`Execution failed: ${(error as Error).message}`) } finally { setIsRunning(false) } } const submitSolution = async () => { if (!problem || !code.trim()) return setIsSubmitting(true) try { const response = await fetch('http://127.0.0.1:5000/api/coding/submit', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ code, problem_id: problem.id }) }) const result = await response.json() if (result.success) { alert(`Solution submitted! Score: ${result.score}% (${result.passed_tests}/${result.total_tests} tests passed)`) } else { alert(`Submission failed: ${result.error}`) } } catch (error) { alert('Failed to submit solution') } finally { setIsSubmitting(false) } } const getDifficultyColor = (difficulty: string) => { switch (difficulty) { case 'Easy': return 'text-green-600 bg-green-100' case 'Medium': return 'text-yellow-600 bg-yellow-100' case 'Hard': return 'text-red-600 bg-red-100' default: return 'text-gray-600 bg-gray-100' } } if (!problem) { return (

Loading problem...

) } return (
{/* Header */}

{problem.title}

{problem.difficulty} {problem.category}
{/* Problem Description */}
{/* Navigation Tabs */}
{(['description', 'examples', 'constraints'] as const).map((tab) => ( ))}
{activeTab === 'description' && (

{problem.description}

)} {activeTab === 'examples' && (

Examples:

{problem.examples.map((example, index) => (
Input: "{example.input}"
Output: "{example.expected}"
{example.description}
))}
)} {activeTab === 'constraints' && (

Constraints:

    {problem.constraints.map((constraint, index) => (
  • {constraint}
  • ))}
)}
{/* Hints Section */} {showHints && (

💡 Hints:

    {problem.hints.map((hint, index) => (
  • {index + 1}. {hint}
  • ))}
)}
{/* Code Editor & Results */}
{/* Code Editor */}

Code Editor

Python