Created
April 27, 2010 18:49
-
-
Save inimino/381140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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