-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscheduleGenerator.py
179 lines (163 loc) · 6.07 KB
/
scheduleGenerator.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import logging
import itertools
import pprint
from functools import cmp_to_key
import data
pp = pprint.PrettyPrinter(indent=4, compact=False)
SHOW = logging.DEBUG
HIDE = logging.CRITICAL
logging.basicConfig(format="%(levelname)s: %(message)s", level=HIDE)
def getAllSectionCombinations(courses: list) -> list:
'''
Return all possible combinations of the sections of different courses
'''
sections = []
for course in courses:
courseSections = []
courseCode = course['courseCode']
for section in course['sections']:
section['courseCode'] = courseCode
courseSections.append(section)
sections.append(courseSections)
sectionCombinations = list(itertools.product(*sections))
return sectionCombinations
def convertHhMmToMinutes(hhMMStr: str) -> int:
'''
Take a time in string in HH:MM format and convert it to minutes
'''
hh, mm = hhMMStr.split(':')
hhInt, mmInt = map(int, [hh, mm])
minutes = hhInt*60 + mmInt
return minutes
def cmpByStartTime(time1: dict, time2: dict) -> int:
'''
A custom comparator function for sorting schedules based on start time
time1: A schedule containing `start` and `end` time in `HH:MM` format
'''
time1Start = time1['start']
time2Start = time2['start']
time1StartInMinutes = convertHhMmToMinutes(time1Start)
time2StartInMinutes = convertHhMmToMinutes(time2Start)
return time1StartInMinutes - time2StartInMinutes
def isOverlappingTimesForSingleDay(times: list) -> bool:
'''
Check if the times are overlapping. `times` contain the schedule of sections
for a single day.
times:
- [{start: '10:10', end: '11:40'}]
'''
sortedTimes = sorted(times, key=cmp_to_key(cmpByStartTime))
for i in range(1, len(sortedTimes)):
if sortedTimes[i-1]['end'] > sortedTimes[i]['start']:
return True
return False
def isOverlappingSchedule(schedulesPerDay: dict) -> bool:
'''
Get schedules per day and check if it has any overlapping schedule.
schedulesPerDay:
- {'Monday: [{start: '10:10', end: '11:40'}]}
'''
for day in schedulesPerDay:
isOverlapping = isOverlappingTimesForSingleDay(schedulesPerDay[day])
if isOverlapping:
return True
return False
def isCombinationOfSectionsValid(sectionCombination: list) -> bool:
'''
Validate a section combination. A combination is valid if it does not
have any overlapping time.
'''
schedulePerDay = {}
for section in sectionCombination:
sectionSchedules = section['sectionSchedule']
labSchedules = section.get('labSchedule', [])
allSchedules = sectionSchedules + labSchedules
for schedule in allSchedules:
day = schedule.get('day')
startTime = schedule.get('start')
endTime = schedule.get('end')
scheduleDict = {
'start': startTime,
'end': endTime
}
if day in schedulePerDay:
schedulePerDay[day].append(scheduleDict)
else:
schedulePerDay[day] = [scheduleDict]
scheduleIsOverlapping = isOverlappingSchedule(schedulePerDay)
return not scheduleIsOverlapping
def getCourseCombinations(courseList: list) -> list:
'''
Get valid section combinations from course list
'''
sectionCombinations = getAllSectionCombinations(courseList)
validCombinations = []
for sectionCombination in sectionCombinations:
combinationValid = isCombinationOfSectionsValid(sectionCombination)
if combinationValid:
validCombinations.append(sectionCombination)
return validCombinations
def printSchedule(sections: list):
'''
Print a basic schedule
'''
schedulePerDay = dict()
for section in sections:
sectionNumber = section['section']
courseCode = section['courseCode']
sectionSchedules = section['sectionSchedule']
labSchedules = section.get('labSchedule', [])
for sectionSchedule in sectionSchedules:
startTime = sectionSchedule['start']
endTime = sectionSchedule['end']
day = sectionSchedule['day']
scheduleDict = {
'start': startTime,
'end': endTime,
'section': sectionNumber,
'courseCode': courseCode
}
if day in schedulePerDay:
schedulePerDay[day].append(scheduleDict)
else:
schedulePerDay[day] = [scheduleDict]
for labSchedule in labSchedules:
startTime = labSchedule['start']
endTime = labSchedule['end']
day = labSchedule['day']
scheduleDict = {
'start': startTime,
'end': endTime,
'section': sectionNumber,
'courseCode': courseCode,
'isLab': True
}
if day in schedulePerDay:
schedulePerDay[day].append(scheduleDict)
else:
schedulePerDay[day] = [scheduleDict]
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday']
for day in days:
scheduleInDay = schedulePerDay.get(day, [])
scheduleInDaySorted = sorted(scheduleInDay, key=cmp_to_key(cmpByStartTime))
print(day)
print('-' * len(day))
for schedule in scheduleInDaySorted:
start = schedule['start']
end = schedule['end']
isLab = schedule.get('isLab')
courseCode = schedule['courseCode']
section = schedule['section']
if isLab:
print(courseCode, section, start, end, '(Lab)')
else:
print(courseCode, section, start, end)
if __name__ == '__main__':
courseDict = data.getTestData()
validSectionCombinations = getCourseCombinations(courseDict)
for i, sectionCombination in enumerate(validSectionCombinations, 1):
header = f'Schedule {i}'
print(header)
print('-' * len(header))
printSchedule(sectionCombination)
print()