Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Functional Python The Lambda Lambada Recursion

Vincent Zamora
Vincent Zamora
3,872 Points

Prerequisite Challenge:

I am have trouble trying to figure this one out. The code below prints out what I think is correct but I get the bummer. Please advise.courses = {'count': 2, 'title': 'Django Basics', 'prereqs': [{'count': 3, 'title': 'Object-Oriented Python', 'prereqs': [{'count': 1, 'title': 'Python Collections', 'prereqs': [{'count':0, 'title': 'Python Basics', 'prereqs': []}]}, {'count': 0, 'title': 'Python Basics', 'prereqs': []}, {'count': 0, 'title': 'Setting Up a Local Python Environment', 'prereqs': []}]}, {'count': 0, 'title': 'Flask Basics', 'prereqs': []}]}

def prereqs(data, pres=None): pres = pres or set() for pre in data['prereqs']: pres.add(pre['title']) for pr in pre['prereqs']: pres.add(pre['title']) return pres

courses.py
courses = {'count': 2,
           'title': 'Django Basics',
           'prereqs': [{'count': 3,
                     'title': 'Object-Oriented Python',
                     'prereqs': [{'count': 1,
                               'title': 'Python Collections',
                               'prereqs': [{'count':0,
                                         'title': 'Python Basics',
                                         'prereqs': []}]},
                              {'count': 0,
                               'title': 'Python Basics',
                               'prereqs': []},
                              {'count': 0,
                               'title': 'Setting Up a Local Python Environment',
                               'prereqs': []}]},
                     {'count': 0,
                      'title': 'Flask Basics',
                      'prereqs': []}]}



def prereqs(data, pres=None):
    pres = pres or set()
    for pre in data['prereqs']:
        pres.add(pre['title'])
        for pr in pre['prereqs']:
            pres.add(pre['title'])
    return pres
Vincent Zamora
Vincent Zamora
3,872 Points

This actually passes this section but I think it could be written better, any advice?

def prereqs(data, pres=None):
    pres = pres or set()
    for pre in data['prereqs']:
        pres.add(pre['title'])
        for pr in pre['prereqs']:
            pres.add(pr['title'])
            for p in pr['prereqs']:
                pres.add(p['title'])
    return pres

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,030 Points

Great job solving it. What if the number of prerequisites is an arbitrary deep stack? Your current code only handles three layers. Looking at the core loop. Since you add the current level titles to the pres set. All you would need to do is *recursively call the routine to get the prerequisites of the lower levels.

# Your loop
    for pre in data['prereqs']:
        pres.add(pre['title'])
        for pr in pre['prereqs']:
            pres.add(pr['title'])
            for p in pr['prereqs']:
                pres.add(p['title'])

# Same loop using recursion
    for pre in data['prereqs']:
        # add titles at this level
        pres.add(pre['title'])
        # update the pres set with the results of each lower level
        pres.update(prereqs(pre, pres))  #pres.add(pre['title'])

See you can follow the logic. Post back if you need more help. Good luck!!