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 trialJoel Price
13,711 PointsNot entirely sure where to start.
I'm not a hundred percent where to start on this one. I need a return list to return the overlap between the courses, but I'm not sure how to do this. At first I thought I could just return an intersection function, but because it's a dictionary, I'm not sure how I'd get the intersection of the (Values? Keys? The ones after the ':' ).
I've checked out the forums and seen other peoples suggestions and read their code, but it still hasn't clicked. I feel like the videos didn't cover this stuff/go into enough detail, or maybe I'm just not paying enough attention, even after going back over the material.
Any guidance is appreciated.
*I don't really have code for this. Everything I tried just seemed to far off to be worth posting.
1 Answer
Nathan Tallack
22,160 PointsSets are amazing! For the first challenge we are going to use a type method called intersection which will return a new set that contains the things present in both sets. For our case we are iterating over the COURSES dictionary values and where there are values that are present in the topics set the resulting set will be greater than 0 length in which case we will append that key to the courses list that we return.
def covers(topics):
courses = []
for key, value in COURSES.items():
if len(topics.intersection(value)) > 0:
courses.append(key)
return courses
For the second challenge we want to return the course that contain ALL of the topics in our set. So we are going to use the type method called difference. In this case if all of the items in our topic set are present in the value set then there would be no difference, so we are looking for a resulting set where the length is zero.
def covers_all(topics):
courses = []
for key, value in COURSES.items():
if len(topics.difference(value)) == 0:
courses.append(key)
return courses
Sets are one of my most favourite parts of Python. The first thing I find myself doing when working with data processing using other languages that lack a comparable type is looking for libraries that give me this type and its methods. They are WONDERFUL! :)