From f6e0176208d88434ab14129b093fb0428646c34c Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sun, 8 Jan 2023 22:39:18 +0000 Subject: [PATCH] FP - Extra2 - Ex3 added --- 1ano/fp/extra2/twitter.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/1ano/fp/extra2/twitter.py b/1ano/fp/extra2/twitter.py index 3c7606d..2441d07 100644 --- a/1ano/fp/extra2/twitter.py +++ b/1ano/fp/extra2/twitter.py @@ -23,3 +23,36 @@ print(twits[0]["text"]) # Algumas mensagens contêm hashtags: print(twits[880]["text"]) + +# A) +word_count: dict[str, int] = {} + +for tweet in twits: + tweet_content: str = tweet["text"] + for word in tweet_content.split(): + if word not in word_count: + word_count[word] = 0 + word_count[word] += 1 + +word_list = list(word_count.keys()) + +print("A)\n" + str(word_list), end="\n\n") + + +# B) + +ordered_list = sorted(word_list, key=lambda t: word_count[t], reverse=True) +print("B)\n" + str(ordered_list), end="\n\n") + + +# C) + +ordered_hashtag_list = [word for word in ordered_list if word.startswith('#')] +print("C)\n" + str(ordered_hashtag_list), end="\n\n") + + +# D) +print("D)\n") +most_used = word_count[ordered_hashtag_list[0]] +for hashtag in ordered_hashtag_list: + print(f"{hashtag:<30} ({word_count[hashtag]:>2}) {'+' * ((word_count[hashtag]*18)//most_used)}")