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

Olivia Earenfight
Olivia Earenfight
3,509 Points

Not sure where to start - Math Set Python

I'm working on the first challenge for the covers challenge and i can't figure out where to start. I understand what the question is asking me to do, but I'm not sure where to even begin writing this code. I know that for every item in the key of these dictionaries I need to look for intersections with the other sets and then return the courses that have those lists, I just can't figure out how to do intersections on lists that aren't defined, but are looped through.

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):
    results = []
    for key in COURSES:
        if topics.intersection(COURSES.[key])
    return results

1 Answer

My solution:

  • Go through the COURSES dictionary using a for loop
  • Get the intersection between the current course's topics and the topics parameter
  • If the intersection contains something, and is not an empty list, that means there's a topic covered in the current course that we want. Therefore, we append the course's name to our list of course names
  • Return the course names
def covers(topics):
    result_courses = []
    for course_name, course_topics in COURSES.items():
        if course_topics.intersection(topics):
            result_courses.append(course_name)
    return result_courses

Solving problems like these take practice, so I wouldn't worry too much! Just do a lot of them, and eventually they'll be as easy as cake! :wink: