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

tyler bucasas
tyler bucasas
2,453 Points

sets.py challenge 2

someone pls help

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

def covers_all{set}:
    list = []
    for keys, values in COURSES.items():
        for topics in set:
            if topics.intersection(values):
                list.append(keys)
    return(list)
Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Why are you looping through "set". The challenge asks you to - 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?

The idea is to return courses that have contains "all of the courses".

tyler bucasas
tyler bucasas
2,453 Points
def covers_all(topics):
    list = []
    for course_name, course_topics in COURSES.items():
        if topics.union(course_topics):
                list.append(course_name)
    return(list)

Dave varmutant so I got rid of the for loop for what was set and switched the intersection function with the union function i still cant seem to pass the challenge, pls help me

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

You're close, but if you used union that returns everything in both sets.

What is desired is to verify that everything in the passed set is in course_topics. I used difference - if I take the difference between topics and course_topics and nothing is returned (falsey) then they are all in course_topics.

Below would work - notice the "not" and "difference":

def covers_all(topics):
    list = []
    for course_name, course_topics in COURSES.items():
        if not topics.difference(course_topics):
            list.append(course_name)
    return(list)

# of course doing it old school where we avoid negatives - probably just me though :)
def covers_all(topics):
    list = []
    for course_name, course_topics in COURSES.items():
        if topics.difference(course_topics):
            pass
        else:
            list.append(course_name)
    return(list)