Skip to content

Instantly share code, notes, and snippets.

@marcelcaraciolo
Created August 26, 2012 22:13
Show Gist options
  • Save marcelcaraciolo/3483885 to your computer and use it in GitHub Desktop.
Save marcelcaraciolo/3483885 to your computer and use it in GitHub Desktop.
count_number_of_friends
def count_number_of_friends(self, key, values):
'''
Count the number of mutual friends.
Input ({[friend1, friend2], [-1, -1]};
{[friend1, friend2],[1, 1]};):
["jonas", "marcel"] -1
["marcel", "maria"] -1
["jose", "marcel"] -1
["amanda", "marcel"] -1
["fabio", "marcel"] 1
["fabiola", "marcel"] 1
["carol", "marcel"] 1
["carol", "marcel"] 1
Output ({[friend1, friend2], numberOfMutualFriends}):
["fabio", "marcel"] 1
["fabiola", "marcel"] 1
["marcel", "patricia"] 1
["marcel", "paula"] 1
["carol", "marcel"] 2
'''
f1, f2 = key
mutual_friends_count = 0
for value in values:
if value == -1:
return
mutual_friends_count += value
yield (f1, f2), mutual_friends_count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment