Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Tweet:
- """Tweet class."""
- def __init__(self, user: str, content: str, time: float, retweets: int):
- """
- Tweet constructor.
- :param user: Author of the tweet.
- :param content: Content of the tweet.
- :param time: Age of the tweet.
- :param retweets: Amount of retweets.
- """
- self.user = user
- self.content = content
- self.time = time
- self.retweets = retweets
- def find_fastest_growing(tweets: list) -> Tweet:
- """
- Find the fastest growing tweet.
- A tweet is the faster growing tweet if its "retweets/time" is bigger than the other's.
- >Tweet1 is 32.5 hours old and has 64 retweets.
- >Tweet2 is 3.12 hours old and has 30 retweets.
- >64/32.5 is smaller than 30/3.12 -> tweet2 is the faster growing tweet.
- :param tweets: Input list of tweets.
- :return: Fastest growing tweet.
- """
- sorted_tweets = sorted(tweets, key=lambda s: s.retweets / s.time)
- for Tweet in sorted_tweets:
- print(Tweet.user, Tweet.content, Tweet.time, Tweet.retweets)
- return Tweet
- def sort_by_popularity(tweets: list) -> list:
- """
- Sort tweets by popularity.
- Tweets must be sorted in descending order.
- A tweet is more popular than the other if it has more retweets.
- If the retweets are even, the newer tweet is the more popular one.
- >Tweet1 has 10 retweets.
- >Tweet2 has 30 retweets.
- >30 is bigger than 10 -> tweet2 is the more popular one.
- :param tweets: Input list of tweets.
- :return: List of tweets by popularity
- """
- sorted_tweets = sorted(tweets, key=lambda s: s.retweets, reverse=True)
- for Tweet in sorted_tweets:
- return sorted_tweets
- if __name__ == '__main__':
- tweet1 = Tweet("@realDonaldTrump", "Despite the negative press covfefe #bigsmart", 1249, 54303)
- tweet2 = Tweet("@elonmusk", "Technically, alcohol is a solution #bigsmart", 366.4, 166500)
- tweet3 = Tweet("@CIA", "We can neither confirm nor deny that this is our first tweet. #heart", 2192, 284200)
- tweets = [tweet1, tweet2, tweet3]
- print(find_fastest_growing(tweets).user) # -> "@elonmusk"
- filtered_by_popularity = sort_by_popularity(tweets)
- print(filtered_by_popularity[0].user) # -> "@CIA"
- print(filtered_by_popularity[1].user) # -> "@elonmusk"
- print(filtered_by_popularity[2].user) # -> "@realDonaldTrump"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement