1

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

azmilhanif
  • 29
  • 5
  • 2
    Small suggestion: you could use a class object to represt each class/lecture in the time table. That would make it easier to understand and improve your approach – Abhinav Mathur Mar 21 '22 at 07:40
  • I'd highly recommend making a separate 'Course' class with attributes for module_id, time_id, lecturer_id, etc. Keeping things organized with a single monolithic class where each attribute holds a list of ID's is very difficult. Once you have that separate 'Course' class that represents a single course, you can check for duplicates and clashes by using sets and dictionaries, after [making your class hashable](https://stackoverflow.com/questions/2909106/whats-a-correct-and-good-way-to-implement-hash). – kcsquared Mar 21 '22 at 07:40
  • Hello, Your question would be a lot easier if you explained the meaning of every field. Data like `[[6224, 'BSC1', 'ST4'], ['LR1'], ['MTM3']]` might make sense in your school, but will be very obscure for some people reading your question. I have no idea what any of those letters or numbers mean. From your statement *"at the same time (MTM3) and same room (LR1)."* I can guess that `MTM3` is a time and `LR1` is a room. Otherwise I wouldn't have known that. When you present your data, please explain what each field is for. Name of the course, time, room, etc. – Stef Mar 21 '22 at 09:29

1 Answers1

1

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