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 trialRodwell Mazambara
1,402 Pointscovers function working fine in workspaces but failing in code challenges. Can't seem to figure out why.
My covers function is working fine in workspaces but failing in code challenges. Can't seem to figure out why. If I call the function like: print(covers({"Ruby"})) I am getting ['Ruby Basics'] and if I do print(covers({"Ruby","Python"})) I am getting ['Ruby Basics', 'Python Basics'] which I suppose is the correct output.
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 (courses_set):
courses_list=[]
for course in courses_set:
for course_title in COURSES:
if course in course_title:
courses_list.append(course_title)
return courses_list
3 Answers
Greg Kaleka
39,021 PointsHi Rodwell,
The idea here is to check for overlapping topics, not that the string passed in is in the course title. You'll see that your code doesn't work if you give it a topic like "booleans" or "arrays". Rather than checking whether the string is in the title, you should check if the passed in set intersects with any of the course sets. Since we're using intersection
(or just the &
operator), we don't need to loop through the passed in set. We'll just check it against each COURSES
' topic set.
Here's how you set it up - I'll let you write the actual set math to do the check. Go back and watch the previous video if you need to .
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):
courses_list=[]
for title in COURSES:
if (do_set_math_here): # You'll want to check COURSES[title] against topic_set
courses_list.append(title)
return courses_list
Hope this makes sense. Let us know if you figure it out!
Cheers
-Greg
Rodwell Mazambara
1,402 PointsHi Greg. I managed to figure it out and it worked. However, the second part of the challenge is not working but I am pretty certain that this code should work:
def covers_all(topic_set):
courses_list=[]
for title in COURSES:
if topic_set.issubset(COURSES[title]):
courses_list.append(title)
return courses_list
Greg Kaleka
39,021 PointsYep that code works just fine. I copy/pasted into the challenge and it passed.
Make sure you leave the covers
function in place, and add this function at the bottom. Give it another try, and if it doesn't work, paste your entire code here (not just what you've added).
Rodwell Mazambara
1,402 PointsThanks Greg. It was a typo on my part. I had typed issubset wrongly.
Greg Kaleka
39,021 PointsGreat - glad you figured it out!