Advertisement
UniQuet0p1

Untitled

Oct 12th, 2020 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 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. sorted_hashtags = []
  70. for Tweet in tweets:
  71. if Tweet.content.find(hashtag) > 0:
  72. sorted_hashtags.append(Tweet)
  73. else:
  74. pass
  75. return sorted_hashtags
  76.  
  77.  
  78. def sort_hashtags_by_popularity(tweets: list) -> list:
  79. """
  80. Sort hashtags by popularity.
  81.  
  82. Hashtags must be sorted in descending order.
  83. A hashtag's popularity is the sum of its tweets' retweets.
  84. If two hashtags are equally popular, sort by alphabet from A-Z to a-z (upper case before lower case).
  85. >Tweet1 has 21 retweets and has common hashtag.
  86. >Tweet2 has 19 retweets and has common hashtag.
  87. >The popularity of that hashtag is 19 + 21 = 40.
  88.  
  89. :param tweets: Input list of tweets.
  90. :return: List of hashtags by popularity.
  91. """
  92. return sorted(tweets, key=lambda tweet: sum(tweet.retweets), reverse=True)[0]
  93.  
  94.  
  95. if __name__ == '__main__':
  96. tweet1 = Tweet("@realDonaldTrump", "Despite the negative press covfefe #bigsmart", 1249, 54303)
  97. tweet2 = Tweet("@elonmusk", "Technically, alcohol is a solution #bigsmart", 366.4, 166500)
  98. tweet3 = Tweet("@CIA", "We can neither confirm nor deny that this is our first tweet. #heart", 2192, 284200)
  99. tweets = [tweet1, tweet2, tweet3]
  100.  
  101. print(find_fastest_growing(tweets).user) # -> "@elonmusk"
  102.  
  103. filtered_by_popularity = sort_by_popularity(tweets)
  104. print(filtered_by_popularity[0].user) # -> "@CIA"
  105. print(filtered_by_popularity[1].user) # -> "@elonmusk"
  106. print(filtered_by_popularity[2].user) # -> "@realDonaldTrump"
  107.  
  108. filtered_by_hashtag = filter_by_hashtag(tweets, "#bigsmart")
  109. print(filtered_by_hashtag[0].user) # -> "@realDonaldTrump"
  110. print(filtered_by_hashtag[1].user) # -> "@elonMusk"
  111.  
  112. sorted_hashtags = sort_hashtags_by_popularity(tweets)
  113. print(sorted_hashtags[0]) # -> "#heart"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement