Created
January 31, 2020 16:20
-
-
Save saswata-dutta/1537c2d8540347845ad0fff9280dcd27 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// given | |
def getFriends(id: Id): Set[Id] = ??? | |
// implement suggestions for friends who have the most mutual friends with user | |
def suggestedFriends(id: Id, limit: Int): Set[Id] = { | |
require(limit > 0) | |
require(id != null) | |
val friends = getFriends(id) | |
val friendsOfFriends = | |
friends.toSeq | |
.flatMap(f => (getFriends(f).diff(friends) - id).toSeq) | |
friendsOfFriends | |
.groupBy(identity) | |
.sortBy(_._2.size)(Ordering[Int].reverse) | |
.take(limit) | |
.map(_._1) | |
.toSet | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment