Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Sets Set Math

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

set intersection challenge

Any advice on the second part of this challenge?

sets.py
COURSES = {
    "Python Basics": {"Python", "functions", "variables",
                      "booleans", "integers", "floats",
                      "arrays", "strings", "exceptions",
                      "conditions", "input", "loops"},
    "Java Basics": {"Java", "strings", "variables",
                    "input", "exceptions", "integers",
                    "booleans", "loops"},
    "PHP Basics": {"PHP", "variables", "conditions",
                   "integers", "floats", "strings",
                   "booleans", "HTML"},
    "Ruby Basics": {"Ruby", "strings", "floats",
                    "integers", "conditions",
                    "functions", "input"}
}
def covers(topics):
    common_list=[]
    for subject,course in COURSES.items():
        common_subject=topics.intersection(course)
        if len(common_subject)>0:
            common_list.append(subject)
    return common_list

def covers_all(topic_set):
    common_list=[]
    for subject,course in COURSES.items():
        if topic_set=topic_set.intersection(course):
            common_list.append(subject)
    return common_list

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 27,510 Points

You are so close! You totally have the right idea.

I will modify it slightly... If you check the lengths of "topic_set" to its intersection with "course" with logical equality you will solve this one.

The reason why I am checking length (and not logical set equality) is that sets are not considered to be ordered. It would work in any version of Python.

def covers_all(topic_set):
    common_list=[]
    for subject,course in COURSES.items():
        if len(topic_set) == len(topic_set.intersection(course)):
            common_list.append(subject)
    return common_list

You can also use set difference and compare to the empty set if you prefer. This works too.

def covers_all(topic_set):
    common_list=[]
    for subject,course in COURSES.items():
        if topic_set - course == set():
            common_list.append(subject)
    return common_list
Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

Thanks! I keep forgetting the '=' and '==' difference.