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

stuck on set math part 2

how can i get my code to work properly

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

def covers_all(topics,*args):
    course_list=[]
    for course,sets in COURSES.items():
        for topic in topics:
            if topics.intersection(sets):
                course_list.append(course)
    return course_list

2 Answers

Finally cracked it .

def covers_all(topics):
    course_list =[]
    topics=set(topics)
    for course, sets in COURSES.items():
        if (topics-sets)==set() :
            course_list.append(course)
    return course_list

Hi Maxmillan, can you please explain how you came up with lines 3,4, and 5, beginning from where you set topics = set(topics). I'm also stuck on the same problem

line 3 - I changed the topics argument into a set so that I could use the set methods. line 4 - The for statement will loop through all items in the dictionary COURSES line 5 - Here the if statement checks if there is a difference between the topics from input and the topics in COURSES. If there is a difference it will mean that the course(in COURSES) doesn't have all the items from the topics we input. line 6 - if there was no difference then that course will be added to the course_list which is then returned

I hope that made a bit of sense.

Okay i see. Thanks Maxmillan