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

Dmitry Karamin
Dmitry Karamin
10,099 Points

i'm stuck with the task

im trying with this code

def prereqs(data, pres=None):
    pres = pres or set()
    if not pres:
        pres.add(data['title'])
    else:
        for x in data:
            print(x)
            pres.add(x['title'])
    return prereqs(data['prereqs'], pres)

but this code raises an error about TypeError in line

return prereqs(data['prereqs'], pres)

after second iteration. so how to make it right? first recursion is list, second - dict, third - list again....

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()
Dmitry Karamin
Dmitry Karamin
10,099 Points

i've added some lines

def prereqs(data, pres=None):
pres = pres or set()
if not pres:
    pres.add(data['title'])
    return prereqs(data['prereqs'], pres)
else:
    if data:
        for x in data:
            pres.add(x['title'])
        for z in data:
            return prereqs(z['prereqs'], pres)

return pres

and output in my IDE

{'Setting Up a Local Python Environment', 'Object-Oriented Python', 'Python Collections', 'Flask Basics', 'Python Basics', 'Django Basics'}

seems to me ok but Treehouse raises na error ? type of output is 'set'

4 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hmm, well, "Django Basics" isn't a prerequisite, so it shouldn't be in the set.

I'm curious why you're doing if not pres. Why are you doing that add only the first time the function runs?

Can you make the function do the same work every time it's called, regardless of the state of pres? Remember, functional programming usually doesn't care about state.

Dmitry Karamin
Dmitry Karamin
10,099 Points

Thanks, Kenneth. I did it =) Fuuuf

I understood about "Django Basic" when i woke up this morning.

I used <code>if not pres</code> to put "Django Basic" in the set.

like this?

def prereqs(data, pres=None):
    pres = pres or set()
    for x in data['prereqs']:
        pres.add(x['title'])
        prereqs(x, pres)
    return pres

or

def prereqs(data, pres=None):
    pres = pres or set()
    for x in data['prereqs']:
        pres.add(x['title'])
        if x is not None:
            prereqs(x, pres)
    return pres

Which one have higher performance?

Hi

This was a tough one and like you I finished everything else and had to come back to it. I don't know if this is the answer Kenneth was looking for and part of it I can't explain exactly what is happening and if Kenneth could outline one question I had would be awesome.

def prereqs(data, pres=None):
    pres = pres or set()
    for course in data['prereqs']:
        pres.add(course['title'])
        prereqs(course, pres)
    return pres 

First off I was just trying to figure out a way to get the answer and finally came with this:

def prereqs(data, pres=None):
    pres = pres or set()
    for x in data['prereqs']:
        pres.add(x['title'])
        for y in x['prereqs']:
            pres.add(y['title'])
    return pres

Obviously realised that the two for loops were exactly the same so gave it a shot and changed it up for the function. What I don't know is how deep this would go. If the next courses had prereqs also that where different would it get them too? Or like the for loop inside the for loop, would it stop there and then to go further would have to enter in another for loop? eg for z in y['prereqs']:

I also don't know if by putting in the function, is the original for loop stopping at each x and running the next section on it's own loop considering it looks like it should be starting the whole loop over again and resetting the function which I don't think it is... else it wouldn't get 'flask basics' from this list

Sorry to raise more questions...... >_<

Dmitry Karamin
Dmitry Karamin
10,099 Points

Hi Chris.

Everything is easier than you describe. First loop checks for title in data['prereqs']

the second loops through the data['prereqs'] to call function recursively

these are two independent loops. if you still don't get the answer, let me know, i'll share it with you.

ELBAZ Yoni
ELBAZ Yoni
6,297 Points

Hey i really tried to find the answer too but it dont seems to work if someone could help it should be great for everybody thanks a lot in advance

def prereqs(data, pres=None):
    pres = pres or set()
    for x["title"] in data["prereqs"]:
        pres.add(x["title"])
    for x["title"] in data["prereqs"]:
        return prereqs(x["title"], pres)
Dmitry Karamin
Dmitry Karamin
10,099 Points

Have a nice day =)

def prereqs(data, pres=None):
    pres = pres or set()
    for x in data['prereqs']:
        pres.add(x['title'])
    for z in data['prereqs']:
        return (prereqs(z, pres))
    return pres
ELBAZ Yoni
ELBAZ Yoni
6,297 Points

Thanks to you Dmitry!

thank you very much God Bless