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 trialLeo Marco Corpuz
18,975 Pointsset intersection challenge
Any advice on the second part of this challenge?
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(topics):
common_list=[]
for subject,course in COURSES.items():
common_subject=topics.intersection(course)
if len(common_subject)>0:
common_list.append(subject)
return common_list
def covers_all(topic_set):
common_list=[]
for subject,course in COURSES.items():
if topic_set=topic_set.intersection(course):
common_list.append(subject)
return common_list
1 Answer
Jeff Muday
Treehouse Moderator 28,720 PointsYou are so close! You totally have the right idea.
I will modify it slightly... If you check the lengths of "topic_set" to its intersection with "course" with logical equality you will solve this one.
The reason why I am checking length (and not logical set equality) is that sets are not considered to be ordered. It would work in any version of Python.
def covers_all(topic_set):
common_list=[]
for subject,course in COURSES.items():
if len(topic_set) == len(topic_set.intersection(course)):
common_list.append(subject)
return common_list
You can also use set difference and compare to the empty set if you prefer. This works too.
def covers_all(topic_set):
common_list=[]
for subject,course in COURSES.items():
if topic_set - course == set():
common_list.append(subject)
return common_list
Leo Marco Corpuz
18,975 PointsLeo Marco Corpuz
18,975 PointsThanks! I keep forgetting the '=' and '==' difference.