fork(2) download
  1. def most_friends_common(user_id, all_users):
  2. """Find which user have the most friends in common with the user."""
  3. def nfriends_common(id, friends=set(all_users[user_id])):
  4. return len(friends.intersection(all_users[id])) if id != user_id else -1
  5. return max(all_users, key=nfriends_common)
  6.  
  7. print(most_friends_common('4444', {
  8. '1111': ['2222', '3333'], # 1111 has 2222, 3333 friends
  9. '2222': ['1111'],
  10. '3333': ['1111', '4444', '5555'],
  11. '4444': ['1111', '2222', '3333'],
  12. '5555': []}))
  13.  
Success #stdin #stdout 0.02s 5740KB
stdin
Standard input is empty
stdout
1111