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 trialSerhat Bolsu
15,328 PointsRecursion in Python Functional Python course, I am really lost in this , and I am also lost in recursions, please help
Recursion in Python Functional Python course, I am really lost in this , and I am also lost in recursions, please help
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()
reduce(
3 Answers
Paul Bentham
24,090 PointsOk, without giving you the answer I'll try and provide a guide:
- You need to loop through your 'data' (in this instance it would be 'courses').
- For each 'course' in 'courses' (or data) you need to add the "title" of the prereqs
Once you've done that, consider 'course' to be your next set of data that you need to loop through to get the next batch of prereqs, so put that into the prereqs function.
Workspaces is your friend on this one, trial and error helps!
Chris Howell's answer here helped me work it out: https://teamtreehouse.com/community/skipping-items-while-building-a-set
Mark Chesney
11,747 PointsIn case a hint is helpful, here's how I got started:
def prereqs(data, pres=None):
pres = pres or set()
list_of_dicts = data['prereqs']
if list_of_dicts:
pass # loop over list_of_dicts
return pres
Tips:
It's helpful for me to name the list
list_of_dicts
according to the datatype.data
iscourses
, which is a dictionary that contains lists of other dictionaries.- You can see that as
prereqs()
is called recursively,data
eventually becomes a dict with an empty list for prereqs. When this occurs, theif
statement will evaluate to false and not execute.
- You can see that as
Good luck!
Alexander Davison
65,469 PointsRecursion and functional programming is an advanced programming concept, so I wouldn't worry too much about not quite understanding it. I recommend taking a more beginner course before taking this course. (Note that this course is marked as "Advanced")