How to make two subjects repeat consecutively while making a timetable generated system

20 Views Asked by At

So i am using randomisation to create a time table generating system for my college . I want all theroritical subjects to be randomly assigned in the timetable and the lab hours to be assigned consecutively. For example if 7-8 am is lab1 i want 9-10 am to be lab 1 too .

print("USE THE FOLLOWING ABBREVIATIONS FOR THE SUBJECTS :")

   print("""COMPUTER NETWORKS                   :CS618A \nCOMPUTER GRAPHICS                :CS618B\nGRAPHICS(LAB)                   :CS6P1\nMAJOR PROJECT(LAB)                   :CS6P2""")

   subnum=["CS618A","CS618B","CS6P1","CS6P2"]

   for i in range(0, len(subnum)):

       sub = input("enter subject :")

       f = int(input("enter frequency :"))

       d = d + f

       for i in range(0, f):

           lst.append(sub)

   if (d > 30):

       print("invalid input")

   else:

       for i in range(0, 30 - d):

           ele = "___"

           lst.append(ele)

   R = 6

   C = 5

   k = int(input("enter number of timetables"))

   for p in range(k):

       lst1 = []

       matrix = []

       m = []

       time = ["8-9   ", "9-10  ", "10-11 ", "11-12 ", "12-1  ", "1-2   "]

       for i in range(R):

           a = []

           for j in range(C):

               item = lst[0]

               a.append(item)

               lst.remove(item)

               lst1.append(item)

           matrix.append(a)

           m = np.array(matrix)

           matrix1 = m.T

           for e in range(5):

               random.shuffle(matrix1[e])

           m1 = np.array(matrix1)

           matrix2 = m1.T

       for m in range(30):

           lst.append(lst1[m])

       print("-----FE ", end="")

       print(p + 1, end="")

       print("------")

       print("TIME  MON  TUE  WED  THU  FRI")

       for i in range(R):

           print(time[i], end="")

           for j in range(C):

               print(matrix2[i][j], end="  ")

           print()

else:

print("INVALID SEMESTER NUMBER")

MY OTPUT LOOKS LIKE THIS: GRAPHICS(LAB) :lab1 MAJOR PROJECT(LAB) :lab2 enter subject :lab1 enter frequency :2 enter subject :lab2 enter frequency :2 enter subject :CS618B enter frequency :4 enter subject :CS618A enter frequency :3 enter number of timetables 1

-----FE 1------

TIME MON TUE WED THU FRI

8-9 ___ ___ ___ ___ CS618B
9-10 ___ ___ lab2 CS618A ___
10-11 CS618A ___ CS618B lab2 ___
11-12 ___ ___ ___ ___ ___
12-1 lab1 CS618B ___ ___ CS618A
1-2 CS618B lab1 ___ ___ ___
I NEED LAB1 TO REPEAT CONSECUTIVELY FOR 2 HOURS IN A DAY . 8-9 AND 9-10 HAS TO BE LAB1 .THE OTHER SUBJECTS LIKE CS18A AND CS618B CAN BE RANDOM

1

There are 1 best solutions below

0
Fanchen Bao On

This is not a complete solution to your problem, but I want to share with you how I would solve this problem.

First Things First

Do not bother with the input or output at the moment. You are still working on the core logic. Input and output can come later.

Core Logic

Your core logic has the following requirements.

  1. If the subject is a lab, it is randomly assigned a start time but we must guarantee that the second hour is also assigned to the same lab.
  2. If the subject is not a lab, it can be randomly assigned to any start time and it lasts only one hour.

Basically, for any non-lab subject, we can just randomly assign it anywhere. For a lab, we can still randomly assign it, but there must be at least two hours remaining in the day.

Data Structure

Matrix is a good data structure for this problem. In fact, you are already kind of using it. You can define a matrix of 6 rows and 5 cols. Each col represents a day in the week. Each row represents a time slot.

Let's say the matrix is initialized with value 0 in all cells, indicating all time slots are open. We set a cell to a non-zero value if that time slot is taken. For instance, if matrix[2][3] has a non-zero value, that means the time slot of Thursday, 9-10 AM is already taken.

Adapt Core Logic to Data Structure

Let's say each subject has a numerical label. By randomly picking an open cell in the matrix and assign a label to that cell, you are assigning the subject of that label to that time slot. This can satisfy the second requirement of the core logic.

How about the first requirement? A lab must last two hours. That means when we assign a lab to the matrix, not only do we pick a cell but we also must make sure the cell beneath it is open as well. So essentially, the task is to randomly pick two vertically adjacent open cells.

I will leave the logic of how to randomly pick an open cell or two vertically adjacent open cells to you. But feel free to ask more questions if you get stuck.

Input Output

Do this after the core logic is resolved. Since now you have a matrix and the core logic will fill the matrix with numeric labels of the subjects, output should be just cosmetics, not difficult.

Input, however, requires more work. What if a user types in gibberish? What if a user's input is out of bound? How would you alert the user about an error? You will have to do input verification if you want the program to be complete. It's a lot more work than you'd expect.

Tips

  • Write functions to achieve each piece coherent logic. Bigger logic is a function built from smaller functions with a little bit of code to glue them together.
  • Test your functions to ensure each piece of logic is correct. It is always a good idea to learn a testing suite. For python, it could be pytest or the built-in unittest