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 trialyannis sousanis
7,199 PointsIm trying to solve this but I canti
Im trying to solve this but I cant
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(prmtr):
list_courses = []
for key , val in COURSES.items():
if prmtr.intersection(val):
list_courses.append(key)
return list_courses
def covers_all(prmtr):
list_courses = []
for key in COURSES:
if prmtr.intersection(COURSES[key]):
list_courses.append(key)
return list_courses
2 Answers
Steven Parker
231,236 PointsThe result of an intersection will always be a subset. But you might want to check if the intersection is the same as set passed in.
Sashi Shah
2,528 PointsYou are almost there. Just a reminder, the parameter (prmtr) for covers_all() is still a similar set as in the previous challenge, but with more than one topic in it. The contents of prmtr will still be found only in the "VALUE" part of the KEY, VALUE combination in the dictionary COURSES, so you will still need to use your third line from covers(prmtr).
The only change you will be making from the previous challenge is verifying that all the items inside prmtr are in the VALUE part of COURSES.items(). The simplest way for this is to check if prmtr is a subset of "val" . The syntax to check if a set A is a subset of a set B is "if A.issubset(B):" Hope this helps.
Jason Anello
Courses Plus Student 94,610 PointsHi Sashi,
I changed your comment to an answer so that it may be voted on and/or selected as Best Answer.
There's different ways that this problem can be solved but you were correct in suggesting issubset
. If the topics passed in are a subset of the course topics, then you know the course covers all those topics.
Sashi Shah
2,528 PointsSashi Shah
2,528 PointsI get it now... The only thing missing in Yannis' s code is the check to see if the result is the same as the prmtr.
If that check is not made, then a call like covers_all("strings", "floats") will return all the 4 courses when it should return only 3 (Java Basics does not have "floats"). A check to see if the intersection subset the same as the parameter ensures that all the items in the prmtr are indeed present in the topics of a Course. Thanks Steven!