Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Yuyang Peng
Yuyang Peng
5,665 Points

I tried my best on sets.py, been struggling for an hour :/ HELP

So we are given a dictionary with key as names and sets as values the purpose is to return a list where all the topics are covered So I thought, yeah right, that means when the length of our argument equals to the number of the name appearing, then we add the topic to our list. apparently something went wrong with my code

def covers_all(arg): result = [] num = 0 num_len = len(arg) for name, course in COURSES.items(): if course & arg: num += 1 print(num, name) if num == num_len: result.append(name) num = 0 return result Hope to get some help. cheers

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(arg):
    list_course = []
    for name, course in COURSES.items():
        if course.intersection(arg):
            list_course.append(name)
    return list_course

def covers_all(arg):
    result = []
    num = 0
    num_len = len(arg)
    for name, course in COURSES.items():
        if course & arg:
            num += 1
            if num == num_len:
                result.append(name)
                num = 0
    return result
Yuyang Peng
Yuyang Peng
5,665 Points

Okay so another bug found, have to reset the value of num every time we go through the for loop

Yuyang Peng
Yuyang Peng
5,665 Points

Okay so the for loop stops when it finds the intersection of the two sets . What I need to do now either to make the (arg) set as a whole, or to run the for loop for the len(arg) times

1 Answer

Yuyang Peng
Yuyang Peng
5,665 Points

So here is my answer, I hope this will help some of you, since I saw a lot of people struggling

def covers_all(arg):

result = []
num = 0
num_len = len(arg)
for name, course in COURSES.items():
    for value in course: # to loop through the whole list to match 
        if value in arg:
            num += 1
            print(num, name)
            if num == num_len:
                result.append(name)
                num = 0
    num = 0 
return result