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

Chastin Davis
PLUS
Chastin Davis
Courses Plus Student 2,299 Points

I'm lost

I am lost on what to do here. I know I need to use the set math, obviously, such as union and intersection. I am not clear on what data type is going into the def covers(dataType). I am thinking I need to combined or use union. But what.union(what_2)? What Am I comparing or matching up?

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"}
}

1 Answer

Charlie Gallentine
Charlie Gallentine
12,092 Points

I don't have enough caffeine in my body to get the code to work with the challenge's filters, however, this code works in the text editor on my computer. I have no idea why it isn't passing the challenge, but hopefully it can lend some direction!

The challenge is asking you to check each item in COURSES and see if the supplied argument fully overlaps it. If the supplied argument fully intersects with the item in COURSES' value set, it should append the name of the course to a list and return it.

def covers(arg): # The function which takes an argument in the form {"item to check for"}
    new_list = [] # Return item declaration
    for course in COURSES: # Looks at each course in the COURSES Dict
        if len(arg & COURSES[course]) == len(arg): # Checks to see if the intersection is the same length as the argument. 
            new_list.append(course)                # This should tell if there is full overlap/intersection [&] symbol
                                        # If there is full overlap, it appends the course title to new_list
    return new_list

Hope that helps at least a little!

Chastin Davis
Chastin Davis
Courses Plus Student 2,299 Points

I'll give it a try Charlie. I see it a lot here on Treehouse as some solutions to some challenges are correct but will reflect as incorrect. I have to just leave the Treehouse and come back moments later. Otherwise, I'm smashing my brain for four hours or so on one challenge. Also does this particular challenge require the use of the functions union or interest or, etc. of sets?

Chastin Davis
Chastin Davis
Courses Plus Student 2,299 Points

the treehouse system tells me this works: def covers(arg): # The function which takes an argument in the form {"item to check for"} new_list = [] # Return item declaration for course in COURSES: # Looks at each course in the COURSES Dict if (arg & COURSES[course]): # Checks to see if the intersection is the same length as the argument. new_list.append(course) # This should tell if there is full overlap/intersection [&] symbol # If there is full overlap, it appends the course title to new_list return new_list

Charlie Gallentine
Charlie Gallentine
12,092 Points

Glad its working, I'm still not sure why last night, but oh well...

This function is still using the intersect of sets function, however, it is using it in shorthand with the & symbol!