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 trialSamuel Piecz
8,143 PointsStuck on Python Collections (New) // Sets - Challenge Task 2 of 2
Hello fellow Pythonistas,
I can't seem to wrap my head around this challenge. One other post has been made on the topic, but it wasn't really ever solved.
The first part of the challenge is still hazy for me. The "for keys, values" part specifically. A push in the right direction would be greatly appreciated! Thanks in advance.
- Sam
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(single_parameter):
empty_list = []
for keys, values in COURSES.items():
if single_parameter.union(values):
empty_list.append(keys)
return empty_list
def covers_all(single_parameter):
new_empty_list = []
for keys, values in COURSES.items():
if single_parameter.intersection(values):
new_empty_list.append(keys)
return new_empty_list
Ismail KOÇ
1,748 PointsIf you still have trouble with second step, check this link.
3 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey Samuel,
You have everything correct, except for the operation being used on the set. I at first used intersection
too, but then I remembered that Kenneth likes for us to use the docs to find new things that he didn't necessarily go over in the videos. So, if you go to the docs, you will find the operation needed to complete task two. I'll give you a hint... it's very very similar to intersection
and will "Test whether every element in the set is in other." (That's a pretty big 'give away'. )
Keep Coding!
Umesh Ravji
42,386 PointsThanks Jason, never even considered doing it that way, so much easier :)
Samuel Piecz
8,143 PointsThank you Jason! I got it to pass using the method Umesh described. I do want to try the challenge again using the method you are alluding to. Based on the docs, I'm guessing you are talking about issuperset.
Umesh Ravji
42,386 PointsI found this one a little tricky too.
You can use the intersection method on sets to make a set of elements that exist in both sets. So you can use this intersection on the input set and the test set to find which test sets result in a set that has the same amount of items as the input set, in this case 2.
if len(input_set.intersection(test_set)) == 2: # len(input_set)
results_list.append(key)
Samuel Piecz
8,143 PointsUmesh to the rescue!
So basically, intersection is searching for elements in Courses that are the same as whatever the input is. I grasp that part.
I guess I got hung up on the fact we had to use len() for the values. Still a little confusing, but at least I know the solution and can keep looking into it until it clicks.
Thanks for the help!
Hasan Ahmad
6,727 PointsThis method is not working for me. It says that task one is no longer passing
def covers(single_parameter):
empty_list = []
for keys, values in COURSES.items():
if single_parameter.union(values):
empty_list.append(keys)
return empty_list
def covers_all(single_parameter):
new_empty_list = []
for keys, values in COURSES.items():
if single_parameter.issuperset(values):
new_empty_list.append(keys)
return new_empty_list ```
Umesh Ravji
42,386 PointsUmesh Ravji
42,386 PointsYou're right about the len() method Sam, I don't think it's needed. I think I was just doing it that way so you could calculate the length once before the looping begins and then use that each time to compare to the length of the result rather than comparing the two sets.