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

Matthew Smith
Matthew Smith
3,083 Points

I don't understand how to solve this with map, or reduce

I was able to solve the problem, but not by using map, or reduce. Should I have been able to do it using map, or reduce?

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'])
        pres = prereqs(pre, pres)
    return pres

edit (updated, so that the prereq's call has all the variables; pasted an earlier version of my solution)

1 Answer

Steven Parker
Steven Parker
231,269 Points

This is specifically an exercise in using recursion, and your solution is correct.

This challenge doesn't require "map" or "reduce", and neither would be very useful for this task.