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

yannis sousanis
yannis sousanis
7,199 Points

Im trying to solve this but I canti

Im trying to solve this but I cant

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(prmtr):
    list_courses = []
    for key , val in COURSES.items():
        if prmtr.intersection(val):
            list_courses.append(key)
    return list_courses 
def covers_all(prmtr):
    list_courses = []
    for key  in COURSES:
        if prmtr.intersection(COURSES[key]):
            list_courses.append(key) 
    return list_courses        

2 Answers

Steven Parker
Steven Parker
231,269 Points

The result of an intersection will always be a subset. But you might want to check if the intersection is the same as set passed in.

I get it now... The only thing missing in Yannis' s code is the check to see if the result is the same as the prmtr.

If that check is not made, then a call like covers_all("strings", "floats") will return all the 4 courses when it should return only 3 (Java Basics does not have "floats"). A check to see if the intersection subset the same as the parameter ensures that all the items in the prmtr are indeed present in the topics of a Course. Thanks Steven!

You are almost there. Just a reminder, the parameter (prmtr) for covers_all() is still a similar set as in the previous challenge, but with more than one topic in it. The contents of prmtr will still be found only in the "VALUE" part of the KEY, VALUE combination in the dictionary COURSES, so you will still need to use your third line from covers(prmtr).

The only change you will be making from the previous challenge is verifying that all the items inside prmtr are in the VALUE part of COURSES.items(). The simplest way for this is to check if prmtr is a subset of "val" . The syntax to check if a set A is a subset of a set B is "if A.issubset(B):" Hope this helps.

Hi Sashi,

I changed your comment to an answer so that it may be voted on and/or selected as Best Answer.

There's different ways that this problem can be solved but you were correct in suggesting issubset. If the topics passed in are a subset of the course topics, then you know the course covers all those topics.