Advertisement
UniQuet0p1

Untitled

Oct 8th, 2020
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. class Tweet:
  2. """Tweet class."""
  3.  
  4. def __init__(self, user: str, content: str, time: float, retweets: int):
  5. """
  6. Tweet constructor.
  7.  
  8. :param user: Author of the tweet.
  9. :param content: Content of the tweet.
  10. :param time: Age of the tweet.
  11. :param retweets: Amount of retweets.
  12. """
  13. self.user = user
  14. self.content = content
  15. self.time = time
  16. self.retweets = retweets
  17.  
  18.  
  19. def find_fastest_growing(tweets: list) -> Tweet:
  20. """
  21. Find the fastest growing tweet.
  22.  
  23. A tweet is the faster growing tweet if its "retweets/time" is bigger than the other's.
  24. >Tweet1 is 32.5 hours old and has 64 retweets.
  25. >Tweet2 is 3.12 hours old and has 30 retweets.
  26. >64/32.5 is smaller than 30/3.12 -> tweet2 is the faster growing tweet.
  27.  
  28. :param tweets: Input list of tweets.
  29. :return: Fastest growing tweet.
  30. """
  31. sorted_tweets = sorted(tweets, key=lambda s: s.retweets / s.time)
  32. for Tweet in sorted_tweets:
  33. print(Tweet.user, Tweet.content, Tweet.time, Tweet.retweets)
  34. return Tweet
  35.  
  36.  
  37. def sort_by_popularity(tweets: list) -> list:
  38. """
  39. Sort tweets by popularity.
  40.  
  41. Tweets must be sorted in descending order.
  42. A tweet is more popular than the other if it has more retweets.
  43. If the retweets are even, the newer tweet is the more popular one.
  44. >Tweet1 has 10 retweets.
  45. >Tweet2 has 30 retweets.
  46. >30 is bigger than 10 -> tweet2 is the more popular one.
  47.  
  48. :param tweets: Input list of tweets.
  49. :return: List of tweets by popularity
  50. """
  51.  
  52. sorted_tweets = sorted(tweets, key=lambda s: s.retweets, reverse=True)
  53. for Tweet in sorted_tweets:
  54. return sorted_tweets
  55.  
  56.  
  57. if __name__ == '__main__':
  58. tweet1 = Tweet("@realDonaldTrump", "Despite the negative press covfefe #bigsmart", 1249, 54303)
  59. tweet2 = Tweet("@elonmusk", "Technically, alcohol is a solution #bigsmart", 366.4, 166500)
  60. tweet3 = Tweet("@CIA", "We can neither confirm nor deny that this is our first tweet. #heart", 2192, 284200)
  61. tweets = [tweet1, tweet2, tweet3]
  62.  
  63. print(find_fastest_growing(tweets).user) # -> "@elonmusk"
  64.  
  65. filtered_by_popularity = sort_by_popularity(tweets)
  66. print(filtered_by_popularity[0].user) # -> "@CIA"
  67. print(filtered_by_popularity[1].user) # -> "@elonmusk"
  68. print(filtered_by_popularity[2].user) # -> "@realDonaldTrump"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement