Advertisement
STANAANDREY

suggest_people_you_may_know

Jan 3rd, 2023
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. import functools
  2.  
  3.  
  4. def suggest_people_you_may_know(graph, user):
  5.     friends = graph[user]
  6.     suggestions = set()
  7.  
  8.     def reducer(acc, elem):
  9.         friendsOfFriend = graph[elem]
  10.  
  11.         def reducer2(acc2, elem2):
  12.             if elem2 != user and elem2 not in friends:
  13.                 suggestions.add(elem2)
  14.         functools.reduce(reducer2, friendsOfFriend, None)
  15.  
  16.     functools.reduce(reducer, friends, None)
  17.     return suggestions
  18.  
  19. g = {
  20. "Ana": ["Andrei", "Bogdan", "Maria"],
  21. "Andrei": ["Ana", "Bogdan", "Robert"],
  22. "Bogdan": ["Ana", "Andrei"],
  23. "Maria": ["Ana"],
  24. "Robert": ["Andrei"]
  25. }
  26. print(suggest_people_you_may_know(g, "Bogdan"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement