-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTTmultipleSection.py
More file actions
229 lines (193 loc) · 8.34 KB
/
Copy pathTTmultipleSection.py
File metadata and controls
229 lines (193 loc) · 8.34 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 13 10:58:52 2018
@author: Siddharth Singh
"""
import random
import xlrd
import xlsxwriter
workbook = xlrd.open_workbook('ttinput.xlsx')
worksheet = workbook.sheet_by_index(0)
workbookOutput = xlsxwriter.Workbook('Timetable.xlsx')
worksheet1 = workbookOutput.add_worksheet('SectionA')
#Reading Number of Sections
NumberOfSections=int(worksheet.cell(2, 2).value)
#print (NumberOfSections)
#Reading Number of Subjects
NumberOfSubjects=int(worksheet.cell(2, 7).value)
#print (NumberOfSubjects)
#Reading Number of Labs
NumberOfLabs=int(worksheet.cell(2, 12).value)
#print (NumberOfLabs)
#Reading Number of Batches per section
NumberOfBatches=int(worksheet.cell(4, 12).value)
#print (NumberOfBatches)
#Creating a dictionary that will hold Subject code as key and list of that subject's teachers as value
TeaDict = {}
for i in range (6,6+NumberOfSubjects):
j=4
temp = []
while (worksheet.cell(i, j).value!=0):
temp.append(worksheet.cell(i, j).value)
j+=1
TeaDict[worksheet.cell(i, 2).value] = temp
#print('Teachers For Each Subject:\n',TeaDict)
#Creating a dictionary that will store Subject code as key and subject credit as value
SubCredit = {worksheet.cell(i, 2).value:int(worksheet.cell(i, 3).value) for i in range (6,6+NumberOfSubjects)}
#print(SubCredit)
#Creating a list of subject Codes
SubList=[]
for i in range (0,NumberOfSubjects): SubList.append(worksheet.cell(6+i, 2).value)
#print(SubList)
#Creating a dictionary that will store Subject code as key and teacher as value for one section
ThisSection = {worksheet.cell(i, 2).value:'NULL' for i in range (6,6+NumberOfSubjects)}
#print(ThisSection)
table = []
def updateTT(sheetno, table=[]):
#creating timetable table and initialising value as zeros except halfday
#print('\n-----Initial-TIME-TABLE-----')
day = 6
hour = 7
table = [[0] * hour for i in range(day)]
table[5][4]=table[5][5]=table[5][6]='X' #halfday
#for i in range (0,6): print(table[i])
#print('-----------------------------')
worksheetSection = workbook.sheet_by_index(sheetno)
#updating TimeTable
#First setting lab timings. Each lab is of 3 hrs. each section has three lab day
#choosing random days from week to set lab timing. number of lab day is equal to NumberOfBatches
labDayFixed=[]
labHours=[1,4]
for i in range (0,NumberOfBatches):
labSet=0
while(labSet==0):
labDay=random.randint(0,5)
if labDay not in labDayFixed:
j=random.choice(labHours)
if labDay==5:
j=1 #on saturday lab can only be in first half
if (worksheetSection.cell(labDay, j).value)!='LAB':
labDayFixed.append(labDay)
table[labDay][j]=table[labDay][j+1]=table[labDay][j+2]='LAB'
labSet=1
#Using two loops to traverse through the time table
for i in range (0,6):
Sub = 'Blank'
for j in range (0,7):
if table[i][j]==0:
check=0
Secondpass=0
Firstpass=1
while(Secondpass==0):
#choosing a random subject from SubList that is not same as previous hour
if(j==0):
a=random.randint(0,NumberOfSubjects-1)
Sub = SubList[a]
else:
while(table[i][j-1]==Sub):
a=random.randint(0,NumberOfSubjects-1)
Sub = SubList[a]
#checking if the sub class is already taken in previous hours
for k in range (0,j):
if(Sub==table[i][k]):
Firstpass=0
#if passes first checkpost: check if subject's credit are remaining
if (Firstpass==1):
if (SubCredit[Sub]!=0):
Secondpass=1
SubCredit.update({Sub:SubCredit[Sub]-1})
else:
Secondpass=0
#Checking if number of trials are not more than number of subjects
check+=1
if(check==NumberOfSubjects):
Sub='Blank'
Secondpass=1
#if passes second check post
if (Secondpass==1):
table[i][j]=Sub
#selecting teacher for a section
if(Sub!='Blank'):
if(ThisSection[Sub]=='NULL'):
Teacher=TeaDict[Sub][random.randint(0,2)]
ThisSection[Sub]=Teacher
print(Sub, ':', ThisSection[Sub])
#if Subject credits remains, find a blank spot such that subject is not in that day and replace it with that subject
for i in range (0,6):
day=0
while (SubCredit[SubList[i]]!=0 and day<6):
if SubList[i] not in table[day]:
hr=0
done=0
while (done==0 and hr<7):
if table[day][hr]=='Blank':
table[day][hr]=SubList[i]
SubCredit.update({SubList[i]:SubCredit[SubList[i]]-1})
done=1
hr+=1
else: day+=1
print('\n--------------------------TIME-TABLE--------------------------')
for i in range (0,6): print(table[i])
print('--------------------------------------------------------------\n')
print('Remaining Subject Credits:\n',SubCredit)
return table
print ('\n:::::: Time Table for Section A ::::::\n')
#for Section A
table = updateTT(0,table = [])
#creating a workbook named workbookOutput and creating worksheet1 named SectionA
cell_format = workbookOutput.add_format({'bold': True, 'font_color': '#ff1814', 'bg_color':'#f4f6f9'})
cell_format2 = workbookOutput.add_format({'valign':'center'})
#creating table format
col = 1
for u in range(0,7):
hour= 'Hour ' + str(u+1)
worksheet1.write(0, col, hour, cell_format)
col += 1
worksheet1.write(1, 0, 'MONDAY', cell_format)
worksheet1.write(2, 0, 'TUESDAY', cell_format)
worksheet1.write(3, 0, 'WEDNESDAY', cell_format)
worksheet1.write(4, 0, 'THURSDAY', cell_format)
worksheet1.write(5, 0, 'FRIDAY', cell_format)
worksheet1.write(6, 0, 'SATURDAY', cell_format)
#writing content in table
row = 1
col = 1
for day in range(0,6):
for hour in range(0,7):
worksheet1.write(row, col, table[day][hour], cell_format2)
col+=1
row += 1
col=1
# for SectionBCreating a dictionary that will store Subject code as key and subject credit as value
SubCredit = {worksheet.cell(i, 2).value:int(worksheet.cell(i, 3).value) for i in range (6,6+NumberOfSubjects)}
#Creating a dictionary that will store Subject code as key and teacher as value for one section
ThisSection = {worksheet.cell(i, 2).value:'NULL' for i in range (6,6+NumberOfSubjects)}
#print(ThisSection)
print ('\n:::::: Time Table for Section B ::::::')
#for Section B
table = updateTT(0,table = [])
worksheet2 = workbookOutput.add_worksheet('SectionB')
cell_format = workbookOutput.add_format({'bold': True, 'font_color': '#ff1814', 'bg_color':'#f4f6f9'})
cell_format2 = workbookOutput.add_format({'valign':'center'})
#creating table format
col = 1
for u in range(0,7):
hour= 'Hour ' + str(u+1)
worksheet2.write(0, col, hour, cell_format)
col += 1
worksheet2.write(1, 0, 'MONDAY', cell_format)
worksheet2.write(2, 0, 'TUESDAY', cell_format)
worksheet2.write(3, 0, 'WEDNESDAY', cell_format)
worksheet2.write(4, 0, 'THURSDAY', cell_format)
worksheet2.write(5, 0, 'FRIDAY', cell_format)
worksheet2.write(6, 0, 'SATURDAY', cell_format)
#writing content in table
row = 1
col = 1
for day in range(0,6):
for hour in range(0,7):
worksheet2.write(row, col, table[day][hour], cell_format2)
col+=1
row += 1
col=1
workbookOutput.close()