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

Sets "cover_all" challenge... I am stuck!

I have tried different ways to solve this and nothing is working. I have read the docs and then reread them, nada! Sigh... help.

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 = []
    for keys, values in COURSES.items():
        if topics.union(values):
            courses.append(keys)
    return courses

def covers_all(single_set):
    all_courses = []
    for keys, values in COURSES.item():
        if single_set.intersection(values):
        all_courses.append(keys)
    return all_courses

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

You are so VERY close in the Set Math Challenge.

:point_right: In the for statement, method typo in "COURSES.item()". Should be "COURSES.items()"

:point_right: Intention error in last if code block. append() statement needs further indentation

I had found the indentation mistake, but totally missed the 's' on items (face palm). So I corrected it...

def covers_all(single_set):
    all_courses = []
    for keys, values in COURSES.items():
        if single_set.intersection(values):
            all_courses.append(keys)
    return all_courses

And now I am getting a "Bummer! Didn't get the right output from `covers_all."

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Ahh... The code is checking for a simple intersection. This will match if any item in single_set exists in the course topics. The comparison needs to be if the course set contains all of the single_set items.

This can be done with a simple >= comparison or using the issubset() or issuperset() methods on the sets.

[See this StackOverflow post for examples]

Hi Nichole,

What you have here with intersection should have been the code needed for task 1. I don't think union should have passed task 1 with the covers function because that's going to include all the courses regardless of topics covered.

The union will always be non-empty as long as both sets are not empty and then your if condition will always be true.

I'll report it to staff to see if it can be looked into.

Chris, thank you immensely. One of the ways I had tried it was with >= but it failed, and well we now know it's because I was missing that damn 's'. Anyhoot, I appreciate your assistance. Have a good evening. :)

It works and is good looking for me:

def covers(topic): return [course for course in COURSES if COURSES[course] & topic]