Python OO Design


  • 1
    B

    """

    class Twitter(object):
        twitte_time = 0
    
    class Twitte(object):
        def __init__(self, id):
            self.time = Twitter.twitte_time
            self.id = id
            self.next = None
            Twitter.twitte_time += 1
        
    class User(object):
        def __init__(self, id):
            self.followee = {id}
            self.id = id
            self.twitte_head = None
        
        def follow(self, id):
            self.followee.add(id)
        
        def unfollow(self, id):
            self.followee.discard(id)
        
        def post(self, id):
            twitte = Twitter.Twitte(id)
            twitte.next = self.twitte_head
            self.twitte_head = twitte
    
    
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.user_map = {}
        
    
    def postTweet(self, userId, tweetId):
        """
        Compose a new tweet.
        :type userId: int
        :type tweetId: int
        :rtype: void
        """
        if userId not in self.user_map:
            self.user_map[userId] = self.User(userId)
        user = self.user_map[userId]
        user.post(tweetId)
    
    def getNewsFeed(self, userId):
        """
        Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
        :type userId: int
        :rtype: List[int]
        """
        res = []
        heap = []
        if userId not in self.user_map:
            self.user_map[userId] = self.User(userId)
        usersId = self.user_map[userId].followee
        
        
        for userId in usersId:
            user = self.user_map[userId]
            if user.twitte_head:
                heapq.heappush(heap, (-user.twitte_head.time, user.twitte_head))
        
        n = 0
        while heap and n <10:
            _, head = heapq.heappop(heap)
            res.append(head.id)
            n += 1
            if head.next:
                heapq.heappush(heap, (-head.next.time, head.next))
        return res
        
    
    def follow(self, followerId, followeeId):
        """
        Follower follows a followee. If the operation is invalid, it should be a no-op.
        :type followerId: int
        :type followeeId: int
        :rtype: void
        """
        if followerId not in self.user_map:
            self.user_map[followerId] = self.User(followerId)
        if followeeId not in self.user_map:
            self.user_map[followeeId] = self.User(followeeId)
        follower = self.user_map[followerId]
        followee = self.user_map[followeeId]
        follower.follow(followeeId)
    
    def unfollow(self, followerId, followeeId):
        """
        Follower unfollows a followee. If the operation is invalid, it should be a no-op.
        :type followerId: int
        :type followeeId: int
        :rtype: void
        """
        if followerId not in self.user_map or followerId == followeeId:
            return
        self.user_map[followerId].unfollow(followeeId)
    

    """

    Provide a Python version for OOD


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.