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

nick schubert
nick schubert
3,535 Points

I would have never been able to figure this out by myself.

Hey there,

During the second task of this challenge I got stuck and had to search the community forums for answers. I came across the covers_all function in one of the threads and it makes sense except for the len part. ( I have added some comments to the parts of the code that I don't understand in the code below. )

As I mentioned in the title, I would have never figured out how to solve this challenge on my own and I have been struggling with the Python Collections challenges. I feel like there's a big gap in my knowledge and I'm wondering if someone perhaps could give me some advice and/or guidance.

Thank you very much!

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


def covers_all(string):
    newlist = []
    for keys, values in COURSES.items():
            if len(string & values) == len(string): # Everything makes sense except for this part.
                # why is the len function used in this instance? 
                newlist.append(keys)
    return newlist

1 Answer

Steven Parker
Steven Parker
231,236 Points

At first glance I find this confusing myself! First off, "string" is perhaps not a good choice for a variable name that represents a set, which is what these functions receive as an argument. It might help to give it another name, perhaps "set1".

But once you understand that these are sets, then the "&" operator is the shorthand for an INTERSECTION of the sets. So what that comparison is doing is checking that the size of the intersection of both sets is the same as the size of the first set. This is a way to check that everything in the first set is covered by the second set. So that would mean that the second set (the "values") "covers all".

While this works, it could be done purely using set math:

            if set1.issubset(values):
            # or just...
            if set1 <= values:
nick schubert
nick schubert
3,535 Points

Thanks for answering!

While this does make sense to me, I am still worried about the fact that I can't figure this out myself during the challenges.

Steven Parker
Steven Parker
231,236 Points

I find some things make sense immediately, others can take a while to "sink in". I wouldn't worry, set math is probably one of the latter for many people.