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 trialPiyush Patel
17,253 PointsNeed help in solving this problem
I'm not sure what should we do? I have done it this way. What is wrong with this code? There is no output option, so I don't understand what is expected.
Thanks in advance
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(set1):
courses = []
for key in COURSES:
set2 = set(COURSES[key])
if set2.intersection(set1) == set1:
courses.append(key)
return courses
4 Answers
Steven Parker
231,269 PointsFor task one, you don't need the coverage to be exact.
So a non-empty intersection is enough in itself to determine if a course covers a topic in the set. The intersection doesn't have to exactly equal the original set.
But this technique might be useful for task 2!
Piyush Patel
17,253 PointsI don't understand in which case does the code fail?
I tried this another code.
def covers(set1):
set2 = set()
found = True
for key in COURSES:
for val in set1:
if val not in COURSES[key]:
found = False
if found:
set2.add(key)
print (set2)
return set2
It exhibits even worse behavior. It sometimes returns correct output, but sometimes returns an empty set(). Almost like an asynchronous programming.
Steven Parker
231,269 PointsYour original code is good, just remove the equality constraint ("== set1
"). With that there, only complete coverage will be detected, but for this task you want to detect any overlap.
Piyush Patel
17,253 PointsOne more thing. Why my code doesn't highlight properly when I use ``` tags. It doesn't show correct color highlighting. What am I missing?
Steven Parker
231,269 PointsOn the starting row of the quote block, put the language code after the marks, like this ```py
Piyush Patel
17,253 PointsOk thanks. and why my code doesn't work? I mean both of the above two to solve this challenge. I mean I tried with few options but it worked. I don't understand what is wrong. and the second code gives me strange behavior; sometimes right output but at times, empty set.
Any comments on that?