Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,441 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!!