import gradio as gr import random # Questions database dsa_questions = { "array": [ ("Find the maximum element in an array", "Use a loop and keep track of max value."), ("Reverse an array", "Use two pointers: start and end, swap elements."), ("Find second largest element", "Track largest and second largest in one pass.") ], "string": [ ("Check if string is palindrome", "Compare string with its reverse."), ("Count vowels in a string", "Loop and check characters."), ("Remove spaces from string", "Use replace or loop and rebuild string.") ], "loop": [ ("Print numbers from 1 to N", "Use a simple for loop."), ("Find sum of first N numbers", "Use loop or formula N*(N+1)/2."), ("Check if number is prime", "Loop from 2 to sqrt(n).") ], "basic": [ ("Swap two numbers", "Use temp variable or XOR."), ("Find factorial", "Use loop or recursion."), ("Check even or odd", "Use modulo operator.") ] } # Function to respond def respond(message, history): msg = message.lower() if "array" in msg: q = random.choice(dsa_questions["array"]) return f"šŸ“Œ Question: {q[0]}\nšŸ’” Hint: {q[1]}" elif "string" in msg: q = random.choice(dsa_questions["string"]) return f"šŸ“Œ Question: {q[0]}\nšŸ’” Hint: {q[1]}" elif "loop" in msg: q = random.choice(dsa_questions["loop"]) return f"šŸ“Œ Question: {q[0]}\nšŸ’” Hint: {q[1]}" elif "basic" in msg: q = random.choice(dsa_questions["basic"]) return f"šŸ“Œ Question: {q[0]}\nšŸ’” Hint: {q[1]}" elif "random" in msg: category = random.choice(list(dsa_questions.keys())) q = random.choice(dsa_questions[category]) return f"šŸŽÆ Random Question:\nšŸ“Œ {q[0]}\nšŸ’” {q[1]}" elif "java" in msg: return "Java is an object-oriented language used for backend, apps, and scalable systems." elif "help" in msg: return ( "Try these:\n" "- 'array question'\n" "- 'string question'\n" "- 'loop question'\n" "- 'basic question'\n" "- 'random question'" ) else: return "Type 'help' to see what I can do." # Launch chatbot gr.ChatInterface(fn=respond).launch()