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 trialGabriëlla Markus
630 Pointssets.py task 2
Hi,
I'm lost here, def covers_all should return a list of courses, where all of the topics that are in your set argument, are part off. My code is working in Idle, it returns a list with the right values. So what am I missing? Why can't I complete this task?
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):
new_list = []
for topic in topics:
for basics in COURSES:
if topic in COURSES[basics]:
new_list.append(basics)
return new_list
def covers_all(topics):
new_list = []
temp_list = []
for basics in COURSES:
for topic in topics:
if topic in COURSES[basics]:
temp_list.append(topic)
if len(topics) == len(temp_list):
if basics not in new_list:
new_list.append(basics)
temp_list = []
else:
temp_list = []
return new_list
4 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsI pasted your code directly into the checker and it passed. Please try again.
Animesh Bhadra
2,391 PointsSince the solution above did not use set's math wanted to post a solution using sets.
def covers(topicSet):
courseList = []
for course in COURSES:
if topicSet.intersection(COURSES[course]):
courseList.append(course)
return courseList
Paul Davis
3,722 PointsThis solution appears to work for myself, although using issubset reduces the complexity a little.
def covers_all(topics):
new_list = []
for course in COURSES:
if topics.issubset(COURSES[course]):
new_list.append(course)
return new_list
Gabriëlla Markus
630 PointsThank you Chris, I don't uderstand what happened before. It passed now.
Marc Muniz
Courses Plus Student 3,315 PointsHi, I'm having trouble with the covers_all function and yours seems to be quite elegant...I don't want to just copy it without understanding it. Would u be so kind as to walk me through it? U lose me at the second if statement o.0
Chris Freeman
Treehouse Moderator 68,441 PointsGabriëlla's solution is interesting. The first for
loop generates a temp_list
of all of the topics
that are in the current course under inspection.
The second if
is checking the length of topics
and match in temp_list
If the lengths are equal then all topics are in the course so it should be added to the new_list
.
I'm curious about the line
if basics not in new_list:
I understand not wanting to append a course twice, but it's not obvious to me that a course could be added twice in the natural flow of the code. I tried the code with this if
line commented out and it still passes!