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
Nichole "Nikki" Carnesi
7,103 PointsSets "cover_all" challenge... I am stuck!
I have tried different ways to solve this and nothing is working. I have read the docs and then reread them, nada! Sigh... help.
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):
courses = []
for keys, values in COURSES.items():
if topics.union(values):
courses.append(keys)
return courses
def covers_all(single_set):
all_courses = []
for keys, values in COURSES.item():
if single_set.intersection(values):
all_courses.append(keys)
return all_courses
3 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsYou are so VERY close in the Set Math Challenge.
In the
for statement, method typo in "COURSES.item()". Should be "COURSES.items()"
Intention error in last
if code block. append() statement needs further indentation
Nichole "Nikki" Carnesi
7,103 PointsChris, thank you immensely. One of the ways I had tried it was with >= but it failed, and well we now know it's because I was missing that damn 's'. Anyhoot, I appreciate your assistance. Have a good evening. :)
natalia iaroslavskaia
7,293 PointsIt works and is good looking for me:
def covers(topic):
return [course for course in COURSES if COURSES[course] & topic]
Nichole "Nikki" Carnesi
7,103 PointsNichole "Nikki" Carnesi
7,103 PointsI had found the indentation mistake, but totally missed the 's' on items (face palm). So I corrected it...
And now I am getting a "Bummer! Didn't get the right output from `covers_all."
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsAhh... The code is checking for a simple intersection. This will match if any item in
single_setexists in the course topics. The comparison needs to be if the course set contains all of thesingle_setitems.This can be done with a simple >= comparison or using the
issubset()orissuperset()methods on the sets.[See this StackOverflow post for examples]
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHi Nichole,
What you have here with
intersectionshould have been the code needed for task 1. I don't thinkunionshould have passed task 1 with thecoversfunction because that's going to include all the courses regardless of topics covered.The union will always be non-empty as long as both sets are not empty and then your
ifcondition will always be true.I'll report it to staff to see if it can be looked into.