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 trialHrithik Sharma
6,090 PointsWhy my answer is wrong?
i have tried my code in vs code it works out fine but it says bummer try again in challenge page can tell where i am wrong
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(topicSet):
result = []
for course, topics in COURSES.items():
if topicSet.issubset(topics):
result.append(course)
return result
def covers_all(topicSet):
result = []
for course, topics in COURSES.items():
if topicSet.intersection(topics):
result.append(course)
return result
1 Answer
Alexander Besse
Full Stack JavaScript Techdegree Graduate 35,115 PointsHi Hrithik!
I've made some changes to your code, hopefully the changes make sense. Feel free to ask more question if you would like a deeper explanation!
def covers(topicSet):
result = []
for course, topics in COURSES.items():
for topic in topicSet:
if topic in topics:
result.append(course)
return result
def covers_all(topicSet):
result = []
for course, topics in COURSES.items():
if topicSet.issubset(topics):
result.append(course)
return result
Happy coding!
Hrithik Sharma
6,090 PointsHrithik Sharma
6,090 Pointsi try your soloution and it works but i don't understand why my solution doesn't work can you explain
Alexander Besse
Full Stack JavaScript Techdegree Graduate 35,115 PointsAlexander Besse
Full Stack JavaScript Techdegree Graduate 35,115 PointsHrithik Sharma,
Absolutely! Let me explain further the two methods you used in your functions.
topicSet.issubset(topics) - The issubset method returns a boolean, representing if all items in list A (topicSet) are in list B (topics).
topicSet.intersection(topics) - The intersection method return a list of strings. In the string is all the strings that were in list A (topicSet) and list B (topics). In python, when a list has items in it, it's considered "truthy". If the list is empty, it's considered "falsey". This allows us to use it in IF Statements.
Looking at the first challenge, we wanted to just know if at least one item in list A (topicSet) is in any of the course lists (list B / topics), therefore we don't want to use issubset; we want to use intersection. If we use issubset here, it will pass over courses that contain at least one of the item from list A, and those courses wont be added to the results array.
On the second challenge, we want to know if all items in list A are in list B, therefore we want to use issubset. If we use intersection here, the results array will contain courses that have at least one item from list A, but we want to make sure ALL items from List A are in the course.
Hopefully this helps, let me know if you'd like further explaination! :) Happy coding.
Hrithik Sharma
6,090 PointsHrithik Sharma
6,090 PointsBest answer dude thank you so much you save my tons of time i was getting really mad now i am fine