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 trial
Jackson Lai
3,538 PointsI need some help for this special question ( Not in treehouse)
There are n kids in the class. As school year has just began, the kids barely know each other. However, the teacher knows that there are already some pairs of friends in the class.
Since all kids are very friendly and amicable, they should quickly come to know each other. If kid a is a friend of kid b and kid b is a friend of kid c, kid a will definitely become a friend of kid c. Obviously, if kid a is a friend of kid b, kid b is in turn a friend of kida.
The teacher wants the class to be as united as possible, so she wants to know how many friends groups there will be in the class. A friends group is a set of kids who are friends to one another. For example, [a, b, c] is a friends group if a is a friend to b, a is a friend to c, and b is a friend to c.
Given the number of kids in the class n and the list of pairs of friends, your task is to calculate the number of friends groups they will form.
Example
For n = 10 and friends = [[0,9], [3,7], [7,9], [5,6], [1,5], [4,2]], the output should be friendGroups(n, friends) = 4.
There will be 4 friendsGroups : [0, 9, 7, 3], [5, 6, 1], [4, 2] and [8]
How should I do this? I was thinking to do it in this way but I don't know what kind of function should I use...
First, check all student in friends list. If friends list don't have the student number , .append(something_missed). check 0 to 9 in [[0,9], [3,7], [7,9], [5,6], [1,5], [4,2]] found that 8 is not in the list friends.append(8) friends = [[0,9], [3,7], [7,9], [5,6], [1,5], [4,2], [8]]
and then check duplicated and merge.