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

Leo Yun Tao
Leo Yun Tao
15,956 Points

Help! what did I do wrong

I am currently stuck in Challenge Task 1 of 2, what did I do wrong with this code?

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(x):
    array = []
    sets = {}
    #loop through the sets of topic
    for topics in COURSES.values():
        # loop through the topic 
        for topic in topics.copy():
            # find the number of sets in the array
            for find in x:
                # if the set is in the topic
                if(find in topic):
                    if find in COURSES["Java Basics"]:
                        array.append("Java Basics")                                  
                    elif find in COURSES["PHP Basics"]:
                        array.append("PHP Basics")
                    elif find in COURSES["Ruby Basics"]:
                        array.append("Ruby Basics")
                    elif find in COURSES["Python Basics"]:
                        array.append("Python Basics")                              
    sets = set(array)
    array = list(sets)
    return array

Try the intersection method for sets.

for topics in COURSES.values():
    if topics.intersection(x):
        # do something because the topic is included

SPOILER ALERT I have posted a complete solution below, but suggest you try this first.

1 Answer

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(topic_set):
    course_has_topics_list = []
    for course in COURSES:
        # print("course: {}".format(course))
        topics = COURSES[course]
        # print("COURSES[{}]: {}".format(course, topics))
        if topics.intersection(topic_set):
            # print("topics intersects topic_set: {} {} \n{}".format(topics, topic_set, topics.intersection(topic_set)))
            course_has_topics_list.append(course)
        else:
            continue
            # print("no intersection between: {} {}".format(topics, topic_set))
    return course_has_topics_list


print(covers({"Python"}))