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 Python Collections (2016, retired 2019) Sets Set Math

intersection vs isdisjoint for sets math code challenge "write a function named covers"

# this code does not work in the code challenge but does work in Anacoda.

def covers(set_of_topics):
    list_of_course = []
    for key in COURSES:
        if COURSES[key].intersection(set_of_topics) == set_of_topics:
            list_of_course.append(key)
    return list_of_course

#this codes works in Anaconda and passes the code challenge-- WHY?

def covers(set_of_topics):
    courses_from_course = []
    for key in COURSES:
        if COURSES[key].isdisjoint(set_of_topics) == False:
            courses_from_course.append(key)
        else:
            continue
    return courses_from_course

1 Answer

andren
andren
28,558 Points

Your first covers function returns courses where all of the items in the passed in set are present. Meaning that if I passed in: {"Python", "strings"} for example then I would get back ['Python Basics']. Since that is the only course that contains both a "Python" section and a "strings" section.

Your second covers function returns courses where any of the items in the passed in set are present. Meaning that if I passed in: {"Python", "strings"} for example then I would get back ['Python Basics', 'Java Basics', 'PHP Basics', 'Ruby Basics']. Since all of those course contain either a "Python" section or a "string" section.

For the first task Treehouse wants a function that behaves like your second cover function, giving back any course that contains any of the items passed in. For the second task where you are asked to create a covers_all function Treehouse actually looks for a function that behaves like your first covers function.

Which means that if you rename your first covers function to covers_all then you can complete the entire challenge with the two functions you have written.