Python Python Collections Sets Set Math

Faisal Rana
Faisal Rana
Python Web Development Techdegree Student 2,008 Points

What is wrong with my code ! I'm stuck here !

Here is the 2nd task of the challenge. Create a new function named covers_all that takes a single set as an argument. Return the names of all of the courses, in a list, where all of the topics in the supplied set are covered.

For example, covers_all({"conditions", "input"}) would return ["Python Basics", "Ruby Basics"]. Java Basics and PHP Basics would be excluded because they don't include both of those topics.

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(a_set_of_topics):
    list_of_courses = []
    for course in COURSES:
        if a_set_of_topics.intersection(COURSES[course]):
            list_of_courses.append(course)
    return list_of_courses
def covers_all(x):
    new_list = []
    for course in COURSES.keys():
        if course.pop("Java Basics", "PHP Basics"):
            new_list.append(course.keys())
    return(new_list)
Frank Genova
Frank Genova
Python Web Development Techdegree Student 12,586 Points

SPOILER ALERT - See comments first. Answer below,

You can make use of the subset comparison here.

# Determine whether one set x1 is a subset of the other x2.
# Either format works

x1.issubset(x2)

x1 <= x2

# Set x1 is considered a subset of another set x2 if every element of x1 is in x2.
# x1.issubset(x2) and x1 <= x2 return True if x1 is a subset of x2

1 Answer

Frank Genova
Frank Genova
Python Web Development Techdegree Student 12,586 Points

SPOILER ALERT - See comments first. Answer below,

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

def covers_all(topic_set):
    course_has_all_topics_list = []
    for course in COURSES:
        topics = COURSES[course]
        if topic_set <= topics:
            course_has_all_topics_list.append(course)
        else:
            # print(topic_set <= topics)
            continue
    return course_has_all_topics_list


# print(covers({"Python"}))
print(covers_all({"conditions", "input"}))