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

Piyush Patel
Piyush Patel
17,253 Points

Need help in solving this problem

I'm not sure what should we do? I have done it this way. What is wrong with this code? There is no output option, so I don't understand what is expected.

Thanks in advance

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(set1):
    courses = []
    for key in COURSES:
        set2 = set(COURSES[key])
        if set2.intersection(set1) == set1:
            courses.append(key)
    return courses

4 Answers

Steven Parker
Steven Parker
229,732 Points

For task one, you don't need the coverage to be exact.

So a non-empty intersection is enough in itself to determine if a course covers a topic in the set. The intersection doesn't have to exactly equal the original set.

But this technique might be useful for task 2!

Piyush Patel
Piyush Patel
17,253 Points

I don't understand in which case does the code fail?

I tried this another code.

def covers(set1):
    set2 = set()
    found = True
    for key in COURSES:
        for val in set1:
            if val not in COURSES[key]:
                found = False
        if found:
            set2.add(key)
    print (set2)
    return set2

It exhibits even worse behavior. It sometimes returns correct output, but sometimes returns an empty set(). Almost like an asynchronous programming.

Steven Parker
Steven Parker
229,732 Points

Your original code is good, just remove the equality constraint ("== set1"). With that there, only complete coverage will be detected, but for this task you want to detect any overlap.

Piyush Patel
Piyush Patel
17,253 Points

One more thing. Why my code doesn't highlight properly when I use ``` tags. It doesn't show correct color highlighting. What am I missing?

Steven Parker
Steven Parker
229,732 Points

On the starting row of the quote block, put the language code after the marks, like this :point_right: ```py

Piyush Patel
Piyush Patel
17,253 Points

Ok thanks. and why my code doesn't work? I mean both of the above two to solve this challenge. I mean I tried with few options but it worked. I don't understand what is wrong. and the second code gives me strange behavior; sometimes right output but at times, empty set.

Any comments on that?