Skip to content

Instantly share code, notes, and snippets.

@inimino
Created April 27, 2010 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save inimino/381140 to your computer and use it in GitHub Desktop.
Save inimino/381140 to your computer and use it in GitHub Desktop.
/*
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