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 trialScott Bailey
13,190 PointsSet Math Task 1/2
Hi again!
I've become stuck on the first task. I've created the code and tested it in the workplaces and it seems to be fine. But anytime I submit it it says it didn't get the correct result.
I'm hoping it's something simple I've missed but any and all help will be really appreciated! I really thought I got this one!
Please ignore my use of "x" instead of a proper name!
The task is "Let's write some functions to explore set math a bit more. We're going to be using this COURSES dict in all of the examples. Don't change it, though!
So, first, write a function named covers that accepts a single parameter, a set of topics. Have the function return a list of courses from COURSES where the supplied set and the course's value (also a set) overlap.
For example, covers({"Python"}) would return ["Python Basics"]."
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(x):
choice = []
for course in COURSES:
if x & COURSES[course] == x:
choice.append(course)
return choice
3 Answers
Steven Parker
231,236 PointsWhen working outside of the challenge, it's easy to misinterpret the objective and get a "false positive".
But you're really close here. The challenge wants you test for "any overlap", which means the sets have any elements in common. But comparing the result of the union with the argument will only be true when it has every element in common, which is a bit too specific for this task. Hint: It might come in handy for the next one.
Scott Bailey
13,190 PointsHi, I re wrote the code to something I tried earlier. It passed the task so thank you for that help. This will sound stupid but I'm not sure why though - I'll share the code here but I don't understand, from what you said, why this code worked and my previous didn't. I feel as though there doing the same thing?
def covers(x):
choice = []
for subject, topics in COURSES.items():
if topics & x:
choice.append(subject)
return choice
I'll keep the code there incase someone responds and can talk me through my fault a bit more! But I think i kind of understand what I did wrong now and why the code had to be the way it is (after playing around more). If someone can explain it to me though just to confirm my own thoughts it would be really appreciated!
Steven Parker
231,236 PointsIt was that equality test, where after you did the set union (&) with x, you tested that for equalty with x. The union itself indicates an overlap, but with the equality test also only a complete subset will produce a "True".
Scott Bailey
13,190 PointsThank you - that makes perfect sense. I really appreciate your help!
Steven Parker
231,236 PointsScott Bailey — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!