Advertisement
UniQuet0p1

Untitled

Oct 11th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. """Twitter."""
  2.  
  3.  
  4. class Tweet:
  5. """Tweet class."""
  6.  
  7. def __init__(self, user: str, content: str, time: float, retweets: int):
  8. """
  9. Tweet constructor.
  10.  
  11. :param user: Author of the tweet.
  12. :param content: Content of the tweet.
  13. :param time: Age of the tweet.
  14. :param retweets: Amount of retweets.
  15. """
  16. self.user = user
  17. self.content = content
  18. self.time = time
  19. self.retweets = retweets
  20.  
  21.  
  22. def find_fastest_growing(tweets: list) -> Tweet:
  23. """
  24. Find the fastest growing tweet.
  25.  
  26. A tweet is the faster growing tweet if its "retweets/time" is bigger than the other's.
  27. >Tweet1 is 32.5 hours old and has 64 retweets.
  28. >Tweet2 is 3.12 hours old and has 30 retweets.
  29. >64/32.5 is smaller than 30/3.12 -> tweet2 is the faster growing tweet.
  30.  
  31. :param tweets: Input list of tweets.
  32. :return: Fastest growing tweet.
  33. """
  34. sorted_tweets = sorted(tweets, key=lambda s: s.retweets / s.time)
  35. for Tweet in sorted_tweets:
  36. print(Tweet.user, Tweet.content, Tweet.time, Tweet.retweets)
  37. return Tweet
  38.  
  39.  
  40. def sort_by_popularity(tweets: list) -> list:
  41. """
  42. Sort tweets by popularity.
  43.  
  44. Tweets must be sorted in descending order.
  45. A tweet is more popular than the other if it has more retweets.
  46. If the retweets are even, the newer tweet is the more popular one.
  47. >Tweet1 has 10 retweets.
  48. >Tweet2 has 30 retweets.
  49. >30 is bigger than 10 -> tweet2 is the more popular one.
  50.  
  51. :param tweets: Input list of tweets.
  52. :return: List of tweets by popularity
  53. """
  54. sorted_tweets = sorted(tweets, key=lambda s: s.time)
  55. sorted_tweets = sorted(sorted_tweets, key=lambda s: s.retweets, reverse=True)
  56. return sorted_tweets
  57.  
  58.  
  59. def filter_by_hashtag(tweets: list, hashtag: str) -> list:
  60. """
  61. Filter tweets by hashtag.
  62.  
  63. Return a list of all tweets that contain given hashtag.
  64.  
  65. :param tweets: Input list of tweets.
  66. :param hashtag: Hashtag to filter by.
  67. :return: Filtered list of tweets.
  68. """
  69.  
  70. sorted_hashtags = (tweets, hashtag)
  71. return sorted_hashtags
  72.  
  73. def sort_hashtags_by_popularity(tweets: list) -> list:
  74. """
  75. Sort hashtags by popularity.
  76.  
  77. Hashtags must be sorted in descending order.
  78. A hashtag's popularity is the sum of its tweets' retweets.
  79. If two hashtags are equally popular, sort by alphabet from A-Z to a-z (upper case before lower case).
  80. >Tweet1 has 21 retweets and has common hashtag.
  81. >Tweet2 has 19 retweets and has common hashtag.
  82. >The popularity of that hashtag is 19 + 21 = 40.
  83.  
  84. :param tweets: Input list of tweets.
  85. :return: List of hashtags by popularity.
  86. """
  87. pass
  88.  
  89.  
  90. if __name__ == '__main__':
  91. tweet1 = Tweet("@realDonaldTrump", "Despite the negative press covfefe #bigsmart", 1249, 54303)
  92. tweet2 = Tweet("@elonmusk", "Technically, alcohol is a solution #bigsmart", 366.4, 166500)
  93. tweet3 = Tweet("@CIA", "We can neither confirm nor deny that this is our first tweet. #heart", 2192, 284200)
  94. tweets = [tweet1, tweet2, tweet3]
  95.  
  96. print(find_fastest_growing(tweets).user) # -> "@elonmusk"
  97.  
  98. filtered_by_popularity = sort_by_popularity(tweets)
  99. print(filtered_by_popularity[0].user) # -> "@CIA"
  100. print(filtered_by_popularity[1].user) # -> "@elonmusk"
  101. print(filtered_by_popularity[2].user) # -> "@realDonaldTrump"
  102.  
  103. filtered_by_hashtag = filter_by_hashtag(tweets, "#bigsmart")
  104. print(filtered_by_hashtag[0].user) # -> "@realDonaldTrump"
  105. print(filtered_by_hashtag[1].user) # -> "@elonMusk"
  106.  
  107. sorted_hashtags = sort_hashtags_by_popularity(tweets)
  108. print(sorted_hashtags[0]) # -> "#heart"
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement