I have a function that generates a random data
def create_Class(self):
for x in mD.get_module_Code:
self.module_ID.append(x)
for j in rM.get_id:
self.room_ID.append(j)
for t in tM.get_timeID:
self.time_ID.append(t)
for i in gP.get_groupSize:
self.number_of_Students.append(i)
for z in mD.get_module_lecturer:
self.lecturer_ID.append(z)
# Random Module
mod = random.choice(range(len(self.module_ID)))
module = self.module_ID[mod]
# course
crs = mD.get_module_Course_ID[mod]
# Random room
rom = random.choice(range(len(self.room_ID)))
room_ID = self.room_ID[rom]
# random time
tim = random.choice(range(len(self.time_ID)))
time_Slot = self.time_ID[tim]
# lecturer
lec = self.lecturer_ID[mod]
self._Class = [[module, crs, lec], [room_ID], [time_Slot]]
return self._Class
Which produce a single random class
[[5019, 'BSC2', 'ST3'], ['LR1'], ['TTM3']]
I then create a function to run the code above 15 times (3 classes x 5 days) to create 1 nested list to represent a timetable.
def create_timetables(self):
# Random classes
self.Slots = [self.create_Sessions() for _ in range(self.number_of_classes)]
return self.Slots
output:
[[[6224, 'BSC1', 'ST4'], ['LR1'], ['MTM3']], [[4222, 'BSC1', 'ST6'], ['LR1'], ['MTM3']], [[4210, 'BSC1', 'ST1'], ['CR1'], ['TTM2']], [[4210, 'BSC1', 'ST1'], ['CR1'], ['FTM3']], [[5019, 'BSC2', 'ST3'], ['LH1'], ['FTM3']], [[6008, 'BSC3', 'ST1'], ['LB1'], ['WTM1']], [[4201, 'BSC1', 'ST1'], ['LH1'], ['THTM2']], [[4227, 'BSC1', 'ST4'], ['CR1'], ['WTM3']], [[4220, 'BSC2', 'ST5'], ['LH2'], ['THTM2']], [[6226, 'BSC3', 'ST6'], ['CR1'], ['FTM3']], [[6226, 'BSC3', 'ST6'], ['LH1'], ['FTM1']], [[5225, 'BSC2', 'ST6'], ['LB1'], ['THTM3']], [[5201, 'BSC2', 'ST2'], ['LH2'], ['FTM5']], [[4202, 'BSC1', 'ST3'], ['LH1'], ['THTM3']], [[4227, 'BSC1', 'ST4'], ['LH2'], ['THTM2']]]
1st question is: How do I count the number of duplicates in the output. For example, [4210, 'BSC1', 'ST1'] appears 2 times and [6226, 'BSC3', 'ST6'] 2 times, [4227, 'BSC1', 'ST4'] appears 2 times and so on.
2nd question: How do I check if there is a different class at the same time and at the same room? For example, the first two class are being held at the same time (MTM3) and same room (LR1). I would like to +1 to the clashes every time this happens
I want to create a scoring system for the timetable and so this is what I did.
def clash_Calculation(self, classes):
clashes = 0
for x in classes:
# if a lecturer is teaching different classes at the same time
if x[0][0] != x[0][0] and x[0][2] == x[0][2] and x[2] == x[2]:
clashes += 1
# if the same group has different class at the same time
if x[0][0] != x[0][0] and x[0][1] == x[0][1] and x[2] == x[2]:
clashes += 1
# if the same group has different class at different same time
if x[0][0] != x[0][0] and x[0][1] == x[0][1] and x[1] != x[1] and x[2] != x[2]:
clashes += 1
# if there is a duplicate class at different room and different times
if x[0][0] == x[0][0] and x[1] != x[1] and x[2] != x[2]:
clashes += 1
# if there is a duplicate class at same room and sane times
if x[0][0] == x[0][0] and x[1] == x[1] and x[2] == x[2]:
clashes += 1
# if there is a duplicate class at same time different room
if x[0][0] == x[0][0] and x[1] != x[1] and x[2] == x[2]:
clashes += 1
# if there is a duplicate class at different time same room
if x[0][0] == x[0][0] and x[1] == x[1] and x[2] != x[2]:
clashes += 1
self.clashes += clashes
return self.clashes
EDIT:
[[5019, 'BSC2', 'ST3'], ['LR1'], ['TTM3']]
5019 - Represent the module mode
'BSC2' - Represents the course code
'ST3' - Represents the lecturer ID
'LR1' - Represents room ID
'TTM3' - Represents timeslot ID
These combines into one nested list which represents a single lecture information
This answer is given according to the output you've provided:
outputs = [[[6224, 'BSC1', 'ST4'], ['LR1'], ['MTM3']], [[4222, 'BSC1', 'ST6'], ['LR1'], ['MTM3']], [[4210, 'BSC1', 'ST1'], ['CR1'], ['TTM2']], [[4210, 'BSC1', 'ST1'], ['CR1'], ['FTM3']], [[5019, 'BSC2', 'ST3'], ['LH1'], ['FTM3']], [[6008, 'BSC3', 'ST1'], ['LB1'], ['WTM1']], [[4201, 'BSC1', 'ST1'], ['LH1'], ['THTM2']], [[4227, 'BSC1', 'ST4'], ['CR1'], ['WTM3']], [[4220, 'BSC2', 'ST5'], ['LH2'], ['THTM2']], [[6226, 'BSC3', 'ST6'], ['CR1'], ['FTM3']], [[6226, 'BSC3', 'ST6'], ['LH1'], ['FTM1']], [[5225, 'BSC2', 'ST6'], ['LB1'], ['THTM3']], [[5201, 'BSC2', 'ST2'], ['LH2'], ['FTM5']], [[4202, 'BSC1', 'ST3'], ['LH1'], ['THTM3']], [[4227, 'BSC1', 'ST4'], ['LH2'], ['THTM2']]]
Question #1: 1st question is: How do I count the number of duplicates in the output.
According to your examples, I assume you're looking for [module, crs, lec]
duplicates:
# I cast tuple in order to be hashable in a set
module_mapper = map(lambda x: tuple(x[0]), outputs)
# Note: you can change the lists to tuples in your class to avoid the casting
# Sets allow only unique elements
unique_modules = set(module_mapper)
# number of duplicates
duplicate_counter = len(xs) - len(unique_modules)
print(duplicate_counter) # result: 3
Question #2: Check if there is a different class at the same time and at the same room
The following is giving a list of different classes which are at the same time and room:
# this is our condition
def filter_condition(x, y):
return x != y and x[1:] == y[1:]
def filterer(classes, acc=[]):
if classes:
c, cs = classes[0], classes[1:]
if c not in acc:
filtered_classes = list(filter(lambda x: filter_condition(c, x), cs))
if filtered_classes:
acc.extend(filtered_classes + [c])
return filterer(cs, acc)
else:
return acc
# results
print(filterer(outputs, []))
# [[[4222, 'BSC1', 'ST6'], ['LR1'], ['MTM3']],
# [[6224, 'BSC1', 'ST4'], ['LR1'], ['MTM3']],
# [[6226, 'BSC3', 'ST6'], ['CR1'], ['FTM3']],
# [[4210, 'BSC1', 'ST1'], ['CR1'], ['FTM3']],
# [[4227, 'BSC1', 'ST4'], ['LH2'], ['THTM2']],
# [[4220, 'BSC2', 'ST5'], ['LH2'], ['THTM2']]]
Final Note: If you use python 10.x
, then you can replace ifs
with match/case
to look cleaner