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 trialAbner Rivera
14,027 PointsAnother 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:
pres.add(data['title'])
prereqs(data['prereqs'], pres)
if type(data) == list:
for dict_item in data:
prereqs(dict_item['prereqs'], pres)
pres.add(dict_item['title'])
return pres
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsHey 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
dict
s 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
dict
s found in the contained"prereq"
- grab
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!!
Abner Rivera
14,027 PointsOh, I see it now. Thanks a lot for the help
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsHey 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.