[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Populating a timetable with subjects

On 01May2019 19:22, brittocj at <brittocj at> wrote:
>We have to populate a timetable with subjects. What would be the best 

That's a pretty open ended question. Often a constraint on generating 
timetables involves ensuring that no 2 subjects use the same timeslot if 
a student might need to take both those subjects. Unless that student is 
Hermoine Grainger.

Also, this looks like homework: we're happy to help, but we tend not to 
write code for you. We'll suggest approaches and answer specific 


>In our sample timetable there are 25 hours (keys), all the FH value should be replaced with random subjects as many as their corresponding hour.
>timetable = [{'A1': "FH", 'B1': "FH", 'C1': "FH", 'D1': "FH", 'E1': "FH"},
>                    {'A2': "FH", 'B2': "FH", 'C2': "FH", 'D2': "FH", 'E2': "FH"},
>                    {'A3': "FH", 'B3': "FH", 'C3': "FH", 'D3': "FH", 'E3': "FH"},
>                    {'A4': "FH", 'B4': "FH", 'C4': "FH", 'D4': "FH", 'E4': "FH"},
>                    {'A5': "FH", 'B5': "FH", 'C5': "FH", 'D5': "FH", 'E5': "FH"}]
>subjects_required_hours = {"S1": 4, "S2": 3, "S3": 5, "S4": 3, "S5": 5, "S6": 5}

Given just the constraint you describe you want to do something like the 

  loop over the keys of the timetable
    for each key, pick a random subject from those available
    fill in that timetable key with the subject
    reduce the hour count for that subject by the length of the 
    timetable slot (1 hour, so just 1)

The definition of "available subjects" above will be those subject with 
more than 0 hours.

You might do that in a few ways. Two examples:

Use "subject_required_hours" (or a copy of it) as your record of 
available subjects. When a subject's hours drop the 0 (when you subtract 
1 above), deleted it from "subject_required_hours" using the "del" 
Python statement. That way the dict only contains available subjects. So 
you just pick randomly from the keys of the dict on each loop iteration.

Alternatively you could treat it like a deck of cards: make an empty 
list, and append to it sufficent copies of the subject names so that it 
looks like:

  ["S1", "S1", "S1", "S1", "S2", "S2", "S2", "S3" .......]

Then in the loop pick a random element from the list, and remove that 

Performance note: deleting an element from anywhere but the end of a 
list is a little expensive, so you might instead copy the last list 
element to the "deleted" location, and delete the last element.

Python has a "random" module for getting random numbers and you can use 
that to choose a random index into an array/list.

If you pick an approach and then encounter more problems, come back and 
ask (by replying to this thread, _not_ by making a totally new message), 
and include both your code and whatever output you're getting, along 
with a description of what's wrong with the output. Always cut/paste 
code and errors messages straight into your message - this list strips 
attachments and non-text.

Cameron Simpson <cs at>