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

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
221,297 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
221,297 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.