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

Faisal Rana
seal-mask
.a{fill-rule:evenodd;}techdegree
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)

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

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"}))