-
-
Save turtle601/39ea232b8b3aa3d9f93180d0e8707dfb to your computer and use it in GitHub Desktop.
우테코 5기 프리코스 1주차 문제 7번
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
class SNSAlgorithm { | |
constructor(user, friends, visitors) { | |
new UsersError(user); | |
new FriendsError(friends); | |
new VisitorsError(visitors); | |
this.user = user; | |
this.friends = friends; | |
this.visitors = visitors; | |
this.scoreBoard = this.makeScoreBoard(); | |
this.friendGraph = this.makeFriendGraph(); | |
this.notRecommandList = this.getNotRecommandList(); | |
} | |
saveFriendGraph(keyFriend, valueFriend, map) { | |
const defaultValue = map.get(keyFriend) || []; | |
map.set(keyFriend, [...defaultValue, valueFriend]); | |
} | |
makeFriendGraph() { | |
const resultMap = new Map(); | |
this.friends.forEach(([ID_A, ID_B]) => { | |
this.saveFriendGraph(ID_A, ID_B, resultMap); | |
this.saveFriendGraph(ID_B, ID_A, resultMap); | |
}); | |
return resultMap; | |
} | |
makeScoreBoard() { | |
return [ | |
...new Set( | |
[...this.friends, ...this.visitors].flatMap((relation) => relation) | |
), | |
].reduce((acc, cur) => ({ ...acc, [cur]: 0 }), {}); | |
} | |
getNotRecommandList() { | |
return [this.user, ...this.friendGraph.get(this.user)]; | |
} | |
isFriend(person) { | |
return new Set([...this.getNotRecommandList()]).has(person); | |
} | |
scroeFriendToFriend() { | |
[...this.friendGraph.get(this.user)] | |
.flatMap((friend) => [...this.friendGraph.get(friend)]) | |
.filter((person) => !this.isFriend(person)) | |
.forEach((person) => (this.scoreBoard[person] += 10)); | |
} | |
scroeVisitor() { | |
this.visitors | |
.filter((person) => !this.isFriend(person)) | |
.forEach((person) => (this.scoreBoard[person] += 1)); | |
} | |
scoreForRecommend() { | |
this.scroeFriendToFriend(); | |
this.scroeVisitor(); | |
} | |
recommend() { | |
this.scoreForRecommend(); | |
return Object.keys(this.scoreBoard) | |
.map((person) => [person, this.scoreBoard[person]]) | |
.filter(([_, score]) => score > 0) | |
.sort((x, y) => y[1] - x[1] || (x[0] < y[0] ? -1 : 1)) | |
.map(([person, _]) => person) | |
.slice(0, 5); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment