Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
userData = {
'person1' : [1,2,3,4],
'person2' : [3,4,5,6]
}
*/
First loop over all the userData once and construct an index.
index=[]
for user in userData
for n in followers
if(!index[n]) index[n]=[]
index[n].push(user)
This is O(n*m), where n is users and m is avg followers.
Next, loop over userData again, and construct your results
results={}
for user in userData
result=[]
for n in user.followers
result[n]=index[n]
results[user]=result
This is O(n*m) again.
Now your results looks like
{
'person1': [,,,['person2'],['person2']]
'person2': [,,,['person1'],['person1']]
}
...but if you want you can invert them to be like you want.
function returns
{
'person1': {'person2': [3, 4]},
'person2': {'person1': [3, 4]}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment