Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import random | |
| from collections import defaultdict | |
| TIME_SLOTS = ["9:00-10:30", "10:30-12:00", "12:00-1:30", "1:30-3:00"] | |
| DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] | |
| def generate_timetable(courses_file, rooms_file): | |
| courses_df = pd.read_csv(courses_file, skipinitialspace=True) | |
| courses_df.columns = courses_df.columns.str.strip() | |
| rooms_df = pd.read_csv(rooms_file, skipinitialspace=True) | |
| rooms_df.columns = rooms_df.columns.str.strip() | |
| rooms_list = rooms_df["Room"].tolist() | |
| timetable_dict = {} | |
| for cls in courses_df["Class"].unique(): | |
| cls_courses = courses_df[courses_df["Class"] == cls].copy() | |
| # Prepare empty timetable | |
| timetable = {day: {slot: None for slot in TIME_SLOTS} for day in DAYS} | |
| # Keep track of teacher and room occupancy to avoid conflicts | |
| teacher_schedule = {day: [] for day in DAYS} | |
| room_schedule = {day: [] for day in DAYS} | |
| for idx, row in cls_courses.iterrows(): | |
| course = row["Course"] | |
| hours = int(row["Hours"]) | |
| teacher = row["Teacher"] | |
| # How many 1.5-hour slots required | |
| slots_needed = hours // 1.5 | |
| if hours % 1.5 != 0: | |
| slots_needed += 1 | |
| slots_assigned = 0 | |
| attempts = 0 | |
| while slots_assigned < slots_needed and attempts < 100: | |
| day = random.choice(DAYS) | |
| slot = random.choice(TIME_SLOTS) | |
| # Check conflicts | |
| if timetable[day][slot] is None and teacher not in teacher_schedule[day] and len(room_schedule[day]) < len(rooms_list): | |
| room = random.choice([r for r in rooms_list if r not in room_schedule[day]]) | |
| timetable[day][slot] = { | |
| "Course": course, | |
| "Teacher": teacher, | |
| "Room": room | |
| } | |
| teacher_schedule[day].append(teacher) | |
| room_schedule[day].append(room) | |
| slots_assigned += 1 | |
| attempts += 1 | |
| timetable_dict[cls] = timetable | |
| return timetable_dict | |