Spaces:
Sleeping
Sleeping
File size: 2,338 Bytes
2ade87f bbf6fad acffcd2 2ade87f 3e75c6e 2ade87f 3e75c6e acffcd2 3e75c6e acffcd2 3e75c6e acffcd2 3e75c6e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 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
|