import gradio as gr # --- ALGORITHM 1: GREEDY ACTIVITY SELECTION --- def solve_schedule(task_input): """ Parses a list of tasks and applies the Activity Selection (Greedy) algorithm. Input format expected per line: "Task Name, StartTime, EndTime" Example: "Math Class, 09:00, 10:00" """ tasks = [] # 1. Parse the input string try: lines = task_input.strip().split('\n') for line in lines: parts = [p.strip() for p in line.split(',')] if len(parts) >= 3: name = parts[0] start = parts[1] end = parts[2] tasks.append({'name': name, 'start': start, 'end': end}) except Exception as e: return f"Error parsing input: {str(e)}" if not tasks: return "No valid tasks found. Please use format: Name, HH:MM, HH:MM" # 2. Sort by finish time (The Greedy Choice Property) # We remove ':' to compare numbers easily (e.g., "10:30" -> 1030) tasks.sort(key=lambda x: int(x['end'].replace(':', ''))) # 3. Select activities selected = [] if tasks: # Always pick the first activity selected.append(tasks[0]) last_finish_time = int(tasks[0]['end'].replace(':', '')) for i in range(1, len(tasks)): current_start_time = int(tasks[i]['start'].replace(':', '')) # If current task starts after or when the last one finished if current_start_time >= last_finish_time: selected.append(tasks[i]) last_finish_time = int(tasks[i]['end'].replace(':', '')) # 4. Format Output output_text = f"Optimal Schedule (Max {len(selected)} items):\n" output_text += "-" * 40 + "\n" for t in selected: output_text += f"• {t['start']} - {t['end']}: {t['name']}\n" return output_text # --- ALGORITHM 2: DP LONGEST COMMON SUBSEQUENCE --- def solve_lcs(text1, text2): """ Calculates similarity using Longest Common Subsequence (Dynamic Programming). """ m = len(text1) n = len(text2) # 1. Initialize DP Table dp = [[0] * (n + 1) for _ in range(m + 1)] # 2. Fill Table for i in range(1, m + 1): for j in range(1, n + 1): if text1[i - 1] == text2[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) # 3. Backtrack to find the sequence index = dp[m][n] lcs_chars = [""] * (index + 1) i, j = m, n while i > 0 and j > 0: if text1[i - 1] == text2[j - 1]: lcs_chars[index - 1] = text1[i - 1] i -= 1 j -= 1 index -= 1 elif dp[i - 1][j] > dp[i][j - 1]: i -= 1 else: j -= 1 lcs_str = "".join(lcs_chars) # 4. Calculate Similarity Percentage max_len = max(m, n) if max(m, n) > 0 else 1 similarity = (dp[m][n] / max_len) * 100 return ( f"Similarity Score: {similarity:.2f}%\n" f"LCS Length: {dp[m][n]}\n" f"Common Sequence: {lcs_str}" ) # --- GRADIO INTERFACE --- # Default values for inputs default_schedule = """Data Structures, 09:00, 10:30 DAA Lab, 10:00, 12:00 Lunch, 12:00, 13:00 Library Study, 12:30, 14:00""" default_text1 = "The quick brown fox jumps over the dog" default_text2 = "The quick red fox jumped over the lazy dog" # FIX: Removed 'theme' argument to prevent version errors with gr.Blocks() as demo: gr.Markdown("# 🎓 Student AlgoToolkit") gr.Markdown("Prototype built for DAA Hackathon using Greedy & DP Algorithms.") with gr.Tabs(): # TAB 1: Scheduler with gr.TabItem("📅 Greedy Scheduler"): gr.Markdown("### Activity Selection Problem") gr.Markdown("Enter tasks in format: `Name, StartTime, EndTime` (24hr format)") with gr.Row(): with gr.Column(): sched_input = gr.Textbox( label="Task List", value=default_schedule, lines=5 ) sched_btn = gr.Button("Optimize Schedule", variant="primary") with gr.Column(): sched_output = gr.Textbox(label="Optimized Result", lines=8) sched_btn.click(fn=solve_schedule, inputs=sched_input, outputs=sched_output) # TAB 2: Comparator with gr.TabItem("📝 Notes Comparator (DP)"): gr.Markdown("### Longest Common Subsequence") gr.Markdown("Compare two texts to find similarity.") with gr.Row(): col1 = gr.Textbox(label="Text A (Original)", value=default_text1, lines=4) col2 = gr.Textbox(label="Text B (Draft)", value=default_text2, lines=4) diff_btn = gr.Button("Compare Texts", variant="primary") diff_output = gr.Textbox(label="Comparison Analysis", lines=4) diff_btn.click(fn=solve_lcs, inputs=[col1, col2], outputs=diff_output) # Launch the app if __name__ == "__main__": demo.launch()