Python Python Collections (2016, retired 2019) Sets Set Math

Md Akibe Hashan
Md Akibe Hashan
2,408 Points

So, first, write a function named covers that accepts a single parameter, a set of topics. Have the function return a li

can some one show me where is my mistake
    "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(str):
    for a in COURSES:
        if str in a:
            return a

1 Answer

Jeff Muday
Jeff Muday
Treehouse Moderator 24,879 Points

This challenge is a little more complex than you would expect. I can see that you do understand tasks 1 and 2. Let's build on that!

The challenge is testing to see if you can do the following tasks:

1. define a function "covers" and send in a set parameter (you got this one)

2. iterate over a set and examine each set member item (both the "course name" and the "course topics")

3. check each item in the COURSES set and see if the intersection of the set you sent in intersects with the course topics. If it does, then add the "course name" to a new set.



def covers(topics):
    cover_set = set() # this will be our return set/list of courses that match
    for a, a_topics in COURSES.items():
        # notice we use COURSES.items() as this will return a (course name) and a_topics (course topics)
        if a_topics.intersection(topics):
            # if the topics intersect, then add a to our set/list of courses that match
    # finally, return the covers_set (turn it in to a list, optional)
    return list(cover_set)