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

Amol Garg
Amol Garg
1,558 Points

Python sets code challenge. Why isn't my code working?

The task is: Let's write some functions to explore set math a bit more. We're going to be using this COURSES dict in all of the examples. Don't change it, though!

So, first, write a function named covers that accepts a single parameter, a set of topics. Have the function return a list of courses from COURSES where the supplied set and the course's value (also a set) overlap.

For example, covers({"Python"}) would return ["Python Basics"].

When I test my code out in an IDE, it works perfectly. 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"}
}

listFound=[]

def covers(topicSet):
    for key in COURSES:
        if topicSet < COURSES[key]:
            listFound.append(key)
    return listFound
Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Try something like

covers({"a"}) 

in your IDE and what do you get?

It looks like you are comparing the input argument with the key value (not the set of topics). Also you are using the less than operator "<" - is that a set operator (union, intersection, difference or symmetric difference)?

Amol Garg
Amol Garg
1,558 Points

The < symbol is finds out if the items in the set on the left is located in the set on the right. I put this program in Pycharm and it works perfectly. Not sure what test is failing. Any other ideas?

1 Answer

Majid Bilal
Majid Bilal
3,558 Points

Hello Amol,

First of all, it's a set against set comparison, and the comparison should be done between the provided set and the value of the dictionary as stated in the problem "the course's value (also a set) overlap". Secondly it's requiring an intersection not the subset the "<" operator is used for a proper subset and we need to use "&" since we are looking for "overlap" that is intersection.

My code looks like this:

def covers(other):
    ret_list = []
    for key, val in COURSES.items():
        if other & val:
            ret_list += [key]
    return ret_list

Hope that clarifies your understanding, thanks.