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 trialChris Grazioli
31,225 PointsChallenge Task 1 of 1 Finish the prereqs function so that it recursively finds all of the prerequisite course titles
I'm totally lost on this end of the course. It seems like the videos are getting flaky and not explaining the core items they are challenging us on. Where did add() come from? It was in the last challenge but NOT mentioned in the video. And the only reason I passed that is because I searched something out in the forum.
Also what the heck is "pres" this is all left field and I really don't wan to spend a half hour looking it up on the python docs with those awful program examples
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()
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsHey Chris Grazioli,
The recursion challenge relies heavily on built in python type set. The two main methods a set
provides is add()
to add a single item, or update()
to add multiple items from an iterable such as a list, tuple, or other set.
The starter code provided for this challenge may be a bit confusing. Theof ariable name "pres" may be read as a contraction PRErequisiteS.
def prereqs(data, pres=None):
pres = pres or set()
There are three parts to note in this code
- the parameter
pres
is optional. If not provided on the function call,pres
is set toNone
- the expression on the right
pres or set()
is anor
of two objects. Anor
expression in Python returns the first non-False item or the last item if all False . So ifpres
is notNone
(that is, it was passed in on the function call) it will be "true" so the expression will evaluate topres
. Ifpres
was not passed into the function, it will beNone
, thus not "true", so theor
expression will evaluate toset()
. - the full express returns
pres
if it was passed it, or a newset()
ifpres
was not passed it.
For a breakdown on how to think through a recursive solution, check out this other Treehouse post.
Post back if you need more help. Good luck!!