language: Python 3 (python-3.2.3)
date: 196 days 16 hours ago
link:
visibility: private
1
2
3
4
5
6
7
8
9
10
11
12
13
def most_friends_common(user_id, all_users):
    """Find which user have the most friends in common with the user."""
    def nfriends_common(id, friends=set(all_users[user_id])):
        return len(friends.intersection(all_users[id])) if id != user_id else -1
    return max(all_users, key=nfriends_common)
 
print(most_friends_common('4444', {
            '1111': ['2222', '3333'], # 1111 has 2222, 3333 friends
            '2222': ['1111'],
            '3333': ['1111', '4444', '5555'],
            '4444': ['1111', '2222', '3333'],
            '5555': []}))