Python Python Collections Sets Set Math

Leo Yun Tao
Leo Yun Tao
5,543 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
Frank Genova
Frank Genova
Python Web Development Techdegree Student 12,586 Points

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

Frank Genova
Frank Genova
Python Web Development Techdegree Student 12,586 Points
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"}))