| import re | |
| from collections import Counter | |
| def parse_timetable(text): | |
| raw_list = [] | |
| subject_counts = Counter() | |
| # Optimized pattern to handle both "DBMS /A /KKD" and "DS /NR" scenarios. | |
| # Group 1: Subject, Group 2: Division (optional), Group 3: Faculty | |
| pattern = r"([A-Z]{2,})\s*/\s*(?:([A-Z]?)\s*/\s*)?([A-Z0-9]{2,})" | |
| matches = re.finditer(pattern, text) | |
| for match in matches: | |
| subject = match.group(1).strip() | |
| faculty = match.group(3).strip() | |
| raw_list.append({"subject": subject, "faculty": faculty}) | |
| subject_counts[subject] += 1 | |
| return raw_list, dict(subject_counts) | |