Created
August 27, 2024 13:24
-
-
Save VipinDevelops/dfda85af404509f43c7c245c52dac4f0 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
# Heap | |
# # Class User | |
# { | |
# userid : id | |
# tweets : [], | |
# followes : | |
# } | |
# # Class Tweet | |
# { | |
# tweetid : id , | |
# timestamp : time | |
# } | |
class Tweet: | |
def __init__(self,tweetId,timestamp) : | |
self.tweetId = tweetId | |
self.timestamp = timestamp | |
class User: | |
def __init__(self,userId): | |
self.userId = userId | |
self.tweets = [] | |
self.followees = set() | |
def postTweet(self,tweetId,timestamp): | |
tweet = Tweet(tweetId,timestamp) | |
self.tweets.append(tweet) | |
pass | |
def follow (self , followeId): | |
self.followees.add(followeId) | |
def unfollow(self,followeId): | |
self.followees.remove(followeId) | |
class Twitter: | |
def __init__(self): | |
self.timestamp = 0; | |
self.users = {} | |
def postTweet ( self,userId,tweetId): | |
if userId not in self.users: | |
user = User(userId) | |
self.users[userId] = user | |
user = self.users[userId] | |
user.postTweet(tweetId,self.timestamp) | |
self.timestamp = self.timestamp+1 | |
def getNewsFeed(self ,userId): | |
if userId not in self.users: | |
return [] | |
# store all tweets | |
tweets = [] | |
# HEAP | |
# Check | |
# TOP value is MIn value and bottom | |
# user = self.users[userId] | |
# # followes | |
# # put all tweets form followes to store | |
# for followeID in user.followees: | |
# # sort based on time | |
# # return top 10 tweets | |
min_heap = [] | |
user = self.users[userId] | |
for tweet in user.tweets: | |
min_heap.push((tweet.timestamp,tweet.tweetid)) | |
if(min_heap.lenght > 10){ | |
min_heap.pop() | |
} | |
for followerId in user.followes: | |
if followerId in self.users : | |
followe = self.users[followerId] | |
for tweet in followe.tweets: | |
min_heap.push((tweet.timestamp,tweet.tweetid)) | |
if min_heap.lenght > 10 | |
min_heap.pop() | |
recent_tweets = [] | |
while min_heap : | |
return [] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment