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

My code seems to work perfectly in workspaces when tested, but the challenge says it is returning incorrect values.

My code seems to be accurately returning a list of classes that cover the topics in the given set when run in workspaces, but when run it the challenge it does not pass. What am I missing?

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):
    courses_covering = []

    for course in COURSES:
        if topics.issubset(COURSES[course]):
            courses_covering.append(course)

    return courses_covering

4 Answers

Here's a hint:

You can to use intersection instead of checking if a set is a sub set of another.

Ok I may really be missing something, but I get the same error with the following code which uses the difference operator:

def covers(topics): courses = []

for key, value in COURSES.items():
    if len(topics - value) == 0:
        courses.append(key)

return courses

and I also tried the following function that uses intersection:

def covers(topics): courses = []

for key, value in COURSES.items():
    if topics.intersection(value) == topics:
        courses.append(key)

return courses

neither of these passed.

implse finite
implse finite
3,106 Points

Hi,

here's my solution, maybe not the best :

def covers(a):
    result = []
    for k, v in COURSES.items():
        b = COURSES[k]
        c = a.intersection(b)
        if len(c) != 0:
            result.append(k)
    return result

Thank you everyone for your help. I see my problem now. I was actually solving the next step in the challenge. I thought that every topic in the given set had to be covered in the class, which is not the case. Well, at least I found three different ways to solve the next problem.