-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTop_N_Buzz_Words.py
31 lines (30 loc) · 1.27 KB
/
Top_N_Buzz_Words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def topNbuzzWords(toys, quotes, topToys):
topWords = dict()
words = list()
for toy in toys:
toy = toy.lower()
for quote in quotes:
quote = quote.lower()
if toy in quote:
try:
wordCount = topWords[toy]
topWords[toy] = wordCount + quote.count(toy)
except:
topWords[toy] = quote.count(toy)
topWords = sorted(topWords.items(), key=lambda topWords: topWords[1], reverse=True)
topWords = topWords[:topToys]
for topWord in range(len(topWords)):
words.append(topWords[topWord][0])
words.sort()
return(words)
if __name__ == "__main__":
toys = ["elsa", "elmo", "legos", "drone", "tablet", "warcraft"]
quotes = ["Elmoa is the hottest of the season! Elmoa will be on every kid's wishlist!",
"The new Elmoa dolls are super high quality",
"Expect the Elsaa dolls to be very popular this year, Elsaa!",
"Elsaa and Elmoa are the toys I'll be buying for my kids, Elsaa is good",
"For parents of older kids, look into buying them a drone",
"Warcraft is slowly rising in popularity ahead of the holiday season"]
topToys = 2
checkToys = topNbuzzWords(toys, quotes, topToys)
print(checkToys)