~fkfd/sections

ff622cfbe2beff5d4d58ca67ffcc11d9cf98bb43 — Frederick Yin 1 year, 6 months ago eed1014
Use recursion for section_choices()
1 files changed, 21 insertions(+), 19 deletions(-)

M main.py
M main.py => main.py +21 -19
@@ 32,28 32,30 @@ def conflict(section1: list, section2: list):


def section_choices(courses: dict):
    # generate all possible section choices
    # return a list of tuples (course_name, sec_name)
    choices = []
    for course_name, course in courses.items():
        if not choices:
            # first course
            choices = [[(course_name, sec_name)] for sec_name in course["sections"]]
        else:
            branches = []
            for choice in choices:
                branches.extend(
                    [
                        choice + [(course_name, sec_name)]
                        for sec_name in course["sections"]
                    ]
                )
            choices = branches
    return choices
    if not courses:
        return []

    # remove one course from courses and store it here
    course_name = list(courses.keys())[0]
    course = courses.pop(course_name)
    if not courses:
        # base case
        # simply return all sections of this course
        return [[(course_name, sec_name)] for sec_name in course["sections"]]
    else:
        # inductive case
        # recurse with all courses but one, then branch out each
        # known choice with sections of said course
        known_choices = section_choices(courses)
        choices = []
        for known in known_choices:
            for sec_name in course["sections"]:
                choices.append(known + [(course_name, sec_name)])
        return choices


def resolve(courses: dict):
    for choice in section_choices(courses):
    for choice in section_choices(courses.copy()):
        if not any(
            [
                conflict(