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

Anders Axelsen
Anders Axelsen
3,471 Points

Task 2/2: How do I find the right intersection?

I am trying to use the operand & to find where key and value overlap.

I am not sure how I find the right way to return a set, the right set.

Task descriptions are in the bottom of this post.

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(param):
    returnable = []
    for course in COURSES:
        if COURSES[course] & param:
            returnable.append(course)
    return returnable

def covers_all(a_set):
    returnable = []
    for course in COURSES:
        for parameters in course:
            if course[a_set] & course:
                returnable.append(course)
    return returnable

Task 1:

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"].

Task 2:

Great work!

OK, let's create something a bit more refined. Create a new function named covers_all that takes a single set as an argument. Return the names of all of the courses, in a list, where all of the topics in the supplied set are covered.

For example, covers_all({"conditions", "input"}) would return ["Python Basics", "Ruby Basics"]. Java Basics and PHP Basics would be exclude because they don't include both of those topics.

1 Answer

Steven Parker
Steven Parker
229,732 Points

The difference from task 1 is the how complete the overlap is.

In both tasks, it makes sense to combine the sets with "&" to get the overlap. And in task 1, it is enough that the overlap set is not empty.

But for task 2, the overlap set must be the same as the "a_set".