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

Another Recursion in Python Functional Python Question

I do get this code to work and to get the courses. But, I apparently need them in some order, but this is set. Can some help me, please? Already spent like half day on this XD. The output I get resembles these: {'Setting Up a Local Python Environment', 'Object-Oriented Python', 'Django Basics', 'Flask Basics', 'Python Collections', 'Python Basics'} or {'Object-Oriented Python', 'Python Collections', 'Python Basics', 'Flask Basics', 'Setting Up a Local Python Environment', 'Django Basics'}

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()
    if type(data) == dict:
        prereqs(data['prereqs'], pres)

    if type(data) == list:
        for dict_item in data:            
            prereqs(dict_item['prereqs'], pres)
    return pres
Chris Freeman
Chris Freeman
Treehouse Moderator 68,029 Points

Hey Abner Rivera, “Django Basics” is not a prerequisite of any course. You need to figure out had to and jest the titles of the prerequisite courses.

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 68,029 Points

Hey Abner Rivera, this is a hard challenge. I approach challenges that might have a recursive solution by first looking for the elements in the data that have the same structure as the whole data set. Much like fractal pictures have elements that look like the larger whole.

In the courses data, the repeated element appears to be a dict of the form:

{'count': int,
 'title': str,
 'prereqs': dict}

Marking this pattern in the courses data below with Capital letters:

courses = {'count': 2,                                                             A
           'title': 'Django Basics',                                               A
           'prereqs': [{'count': 3,                                                A   B
                     'title': 'Object-Oriented Python',                            A   B
                     'prereqs': [{'count': 1,                                      A   B   D
                               'title': 'Python Collections',                      A   B   D
                               'prereqs': [{'count':0,                             A   B   D
                                         'title': 'Python Basics',                 A   B   D
                                         'prereqs': []}]},                         A   B   D
                              {'count': 0,                                         A   B   E
                               'title': 'Python Basics',                           A   B   E
                               'prereqs': []},                                     A   B   E
                              {'count': 0,                                         A   B   F
                               'title': 'Setting Up a Local Python Environment',   A   B   F
                               'prereqs': []}]},                                   A   B   F
                     {'count': 0,                                                  A   C
                      'title': 'Flask Basics',                                     A   C
                      'prereqs': []}]}                                             A   C

You might think that recursion might be:

  • grab title
  • recurse on dicts found in "prereqs"

But this has the side effect of capturing the top title which we do not want.

Instead, try the pattern:

  • look for "prereqs"
  • for each dict in "prereqs"
    • grab "title"
    • recurse on any dicts found in the contained "prereq"

Be sure to pres.add() or pres.update() as needed, and return pres at the end of the function.

The flow would then go as follows:

  • Parsing courses (top level A)
    • find prereqs (B, C)
    • Grab B title
    • Recurse on B prereqs
      • find prereqs (D, E, F)
      • Grab D title
      • Recurse on D prereqs
        • find empty prereqs
        • return empty pres
      • Grab E title
      • Recurse on E prereqs
        • find empty prereqs
        • return empty pres
      • Grab F title
      • Recurse on F prereqs
        • find empty prereqs
        • return empty pres
    • Grab C title
    • Recurse on C prereqs
      • find empty prereqs
      • return empty pres

Post back if you need more help. Good luck!!

Oh, I see it now. Thanks a lot for the help