-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path355_medium_[implementation]_design_tweeter.py
85 lines (66 loc) · 2.51 KB
/
355_medium_[implementation]_design_tweeter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from typing import List
class Twitter:
def __init__(self):
# TODO: it should be encapsulized
self._Users = { "byId": {}, "allIds": [] } # userId
self._Following = { "byId": {}, "allIds": [] } # followerId, followeeId
self._UserTweets = [] # userId, tweetId, createdAt
self._createdAt = 0
def postTweet(self, userId: int, tweetId: int) -> None:
# FIXME: this method should not know the inner structure of Users, UserTweets, ...
# use interface only
if self._Users["byId"].get(userId) is None:
self._Users["byId"][userId] = None
self._Users["allIds"].append(userId)
# this should be transaction
# multithreading environment, it is not safe
self._UserTweets.append({ "userId": userId, "tweetId": tweetId, "createdAt": self._createdAt })
self._createdAt += 1
def getNewsFeed(self, userId: int) -> List[int]:
allFollowees = self._getAllFollowees(userId)
return self._getRecent10TweetIds(allFollowees)
def follow(self, followerId: int, followeeId: int) -> None:
if self._Following["byId"].get(followerId) is None:
self._Following["byId"][followerId] = []
self._Following["allIds"].append(followerId)
self._Following["byId"].get(followerId).append(followeeId)
def unfollow(self, followerId: int, followeeId: int) -> None:
if followerId == followeeId:
return
allFollowees = set()
if self._Following["byId"].get(followerId):
allFollowees = set(self._Following["byId"].get(followerId))
if followeeId in allFollowees:
self._Following["byId"].get(followerId).remove(followeeId)
def _getAllFollowees(self, followerId: int) -> List[int]:
# including me
if self._Following["byId"].get(followerId) is None:
return [followerId]
return [followerId] + self._Following["byId"].get(followerId)
def _getRecent10TweetIds(self, userIds: List[int]) -> List[int]:
userIds = set(userIds)
tweetIds = []
count = 0
for tweet in self._UserTweets[::-1]:
userId = tweet["userId"]
if userId in userIds:
tweetIds.append(tweet["tweetId"])
count += 1
if count >= 10:
return tweetIds
return tweetIds
twitter = Twitter()
twitter.postTweet(1, 2)
twitter.getNewsFeed(1)
twitter.postTweet(2, 3)
twitter.follow(1, 2)
twitter.postTweet(3, 8)
twitter.follow(1, 3)
print(twitter.getNewsFeed(1))
twitter.unfollow(1, 2)
print(twitter.getNewsFeed(3))
print(twitter._Users)
print(twitter._Following)
print(twitter._UserTweets)
print(twitter._Following)
print(twitter._createdAt)