Below is an example of a program that generates a time table based on provided data of students, courses, instructors, venue, working hours (university timings) and working days.
What is the Python program?
import pandas as pd
class TimetableGenerator:
def __init__(self, students, courses, instructors, venues, working_hours, working_days):
self.students = students
self.courses = courses
self.instructors = instructors
self.venues = venues
self.working_hours = working_hours
self.working_days = working_days
def generate_timetable(self):
timetable = pd.DataFrame(columns=['Day', 'Time', 'Course', 'Instructor', 'Venue'])
for course in self.courses:
# Find available time slots for the course
available_time_slots = self.get_available_time_slots(course)
for time_slot in available_time_slots:
# Check if there is an instructor available for the course at the given time slot
instructor = self.get_available_instructor(course, time_slot)
if instructor is not None:
# Check if there is a venue available at the given time slot
venue = self.get_available_venue(time_slot)
if venue is not None:
# Add the course to the timetable
timetable = timetable.append({
'Day': time_slot.day,
'Time': time_slot.time,
'Course': course.name,
'Instructor': instructor.name,
'Venue': venue.name
}, ignore_index=True)
return timetable
def get_available_time_slots(self, course):
# Filter time slots based on course duration and working hours
available_time_slots = self.working_hours.copy()
available_time_slots.loc[:, 'end_time'] = available_time_slots['start_time'] + pd.Timedelta(minutes=course.duration)
available_time_slots = available_time_slots[available_time_slots['end_time'] <= self.working_hours['end_time']]
# Filter time slots based on working days
available_time_slots = available_time_slots[available_time_slots['day'].isin(self.working_days)]
return available_time_slots
def get_available_instructor(self, course, time_slot):
# Find available instructors for the course
available_instructors = self.instructors.copy()
available_instructors = available_instructors[available_instructors['courses'].contains(course.name)]
# Filter instructors based on time availability
available_instructors = available_instructors[~available_instructors['timetable'].isin(timetable[(timetable['Day'] == time_slot.day) & (timetable['Time'] == time_slot.time)])]
# Select a random instructor from the available list
if len(available_instructors) > 0:
return available_instructors.sample(1).iloc[0]
else:
return None
def get_available_venue(self, time_slot):
# Find available venues
available_venues = self.venues.copy()
# Filter venues based on time availability
available_venues = available_venues[~available_venues['timetable'].isin(timetable[(timetable['Day'] == time_slot.day) & (timetable['Time'] == time_slot.time)])]
# Select a random venue from the available list
if len(available_venues) > 0:
return available_venues.sample(1).iloc[0]
else:
return None