Python : generate certain combination from values in lists of dicts for timetable planning

Python : generate certain combination from values in lists of dicts for timetable planning



I am trying to build a Python program that helps me arrange my timetable so I get most day offs (least school days) for university.



The user shall input a number of courses (Course A, Course B, Course C) and they will receive a list of combinations suggested that will give them least school days without time clash(eg. Course A (L1,T1) Course B(L3,T3B) Course C(L2,T2C))



I scraped some information about courses from my university website and I am now stuck.. here is a sample of what I scraped.


'Lectures':'L1 (2196)': 'Mo04:30PM-05:50PM Fr12:00PM-01:20PM', 'Tutorial': 'T1 (2198)': 'Th06:00PM-06:50PM', 'T2 (2200)': 'Mo03:00PM-03:50PM', 'Lab':
'Lectures': 'L1 (2201)': 'Tu09:00AM-10:20AM Th09:00AM-10:20AM', 'L2 (2203)': 'Tu12:00PM-01:20PM Th12:00PM-01:20PM', 'L3 (2205)': 'Tu03:00PM-04:20PM Th03:00PM-04:20PM', 'L4 (2207)': 'Tu01:30PM-02:50PM Th01:30PM-02:50PM', 'L5 (2209)': 'Tu10:30AM-11:50AM Th10:30AM-11:50AM', 'L6 (2211)': 'Tu04:30PM-05:50PM Th04:30PM-05:50PM', 'Tutorial': 'T1A (2213)': 'Mo05:30PM-06:20PM', 'T1B (2215)': 'We06:00PM-06:50PM', 'T2A (2216)': 'Fr12:00PM-12:50PM', 'T2B (2217)': 'Fr01:30PM-02:20PM', 'T3A (2218)': 'We04:30PM-05:20PM', 'T3B (2219)': 'Th12:00PM-12:50PM', 'T4A (2220)': 'Fr03:30PM-04:20PM', 'T4B (2221)': 'Mo02:00PM-02:50PM', 'T5A (2222)': 'Fr12:00PM-12:50PM', 'T5B (2223)': 'Mo06:00PM-06:50PM', 'T6A (2224)': 'We06:00PM-06:50PM', 'T6B (2225)': 'Mo02:00PM-02:50PM', 'Lab':



The outtermost dictionary are the courses, inside are three dictionaries naming "Lectures","Tutorial","Lab", not all courses have all three of these, but you have to arrange it to a timetable whenever one or more session exists. I want to create a combination of these courses then check if time clash occurs and if yes trash those combination. However, I am not sure how could I create such combinations to ensure that.



EDIT



My ultimate goal is for a list like this:


course A='L':'L1':'Time','L2':'Time','T':'T1':'Time','T2':'Time','LAB':'LAB1':'Time
course B='L':'L1':'Time','T':'T1':'Time','T2':'Time','LAB':'LAB1':'Time



I would want a combination like


CourseA(L1,T1,LAB1)CourseB(L1,T1)
CourseA(L1,T2,LAB1)CourseB(L1,T1)
CourseA(L2,T1,LAB1)CourseB(L1,T1)
CourseA(L1,T1,LAB1)CourseB(L1,T2)
CourseA(L1,T2,LAB1)CourseB(L1,T2)
CourseA(L2,T1,LAB1)CourseB(L1,T2)



to be given, then maybe I will further filter out those that have time clash by tracing back to the sessions' value(time).





Can you please provide a desired output?
– U9-Forward
Aug 23 at 3:52





To edit: stackoverflow.com/posts/51978080/edit
– U9-Forward
Aug 23 at 3:55





I don't fully understand your question
– U9-Forward
Aug 23 at 4:01






edited thanks..
– Benjamin Trolio
Aug 23 at 4:31




1 Answer
1



Generating permutations for nested lists like that is a bit tricky.



Just to get you started here's how you could go about generating all the possible permutations for some nested lists. You need to try to figure out how you can apply this pattern to your problem.


import itertools
# a list with 4 categories
a = [[1,2,3],[4,5,6],[7,8,9], [10,11,12]]

# this function will help generate permutations
prod = itertools.product

# combine the first two columns. The for loop below assumes
# that the previous result is a list.
result = list(prod(a[0],a[1]))

for x in a[2:]:
result = list(prod(x, result))
result = [[r[0]] + list(r[1]) for r in result]

print(result)



The output of this program is:


[[10, 7, 1, 4], [10, 7, 1, 5], [10, 7, 1, 6], [10, 7, 2, 4], [10, 7, 2, 5],
[10, 7, 2, 6], [10, 7, 3, 4], [10, 7, 3, 5], [10, 7, 3, 6], [10, 8, 1, 4], ...
[12, 9, 1, 5], [12, 9, 1, 6], [12, 9, 2, 4], [12, 9, 2, 5], [12, 9, 2, 6],
[12, 9, 3, 4], [12, 9, 3, 5], [12, 9, 3, 6]]



Now you would want to generate permutations representing each class, and then do the same thing with those lists.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)