Advertisement
UniQuet0p1

Untitled

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