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 trialYuyang Peng
5,665 PointsI tried my best on sets.py, been struggling for an hour :/ HELP
So we are given a dictionary with key as names and sets as values the purpose is to return a list where all the topics are covered So I thought, yeah right, that means when the length of our argument equals to the number of the name appearing, then we add the topic to our list. apparently something went wrong with my code
def covers_all(arg): result = [] num = 0 num_len = len(arg) for name, course in COURSES.items(): if course & arg: num += 1 print(num, name) if num == num_len: result.append(name) num = 0 return result Hope to get some help. cheers
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(arg):
list_course = []
for name, course in COURSES.items():
if course.intersection(arg):
list_course.append(name)
return list_course
def covers_all(arg):
result = []
num = 0
num_len = len(arg)
for name, course in COURSES.items():
if course & arg:
num += 1
if num == num_len:
result.append(name)
num = 0
return result
Yuyang Peng
5,665 PointsOkay so the for loop stops when it finds the intersection of the two sets . What I need to do now either to make the (arg) set as a whole, or to run the for loop for the len(arg) times
1 Answer
Yuyang Peng
5,665 PointsSo here is my answer, I hope this will help some of you, since I saw a lot of people struggling
def covers_all(arg):
result = []
num = 0
num_len = len(arg)
for name, course in COURSES.items():
for value in course: # to loop through the whole list to match
if value in arg:
num += 1
print(num, name)
if num == num_len:
result.append(name)
num = 0
num = 0
return result
Yuyang Peng
5,665 PointsYuyang Peng
5,665 PointsOkay so another bug found, have to reset the value of num every time we go through the for loop