Advertisement
Guest User

sol1

a guest
Feb 23rd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. import re
  2. class Solution():
  3.     def topNBuzzWords(self,numToys, topToys,toys,numQuotes,quotes):
  4.         cnt = { toy: [0,0] for toy in toys}
  5.  
  6.         for quote in quotes:
  7.             is_updated = {toy: False for toy in toys}
  8.             for word in quote.lower().split():
  9.                 word = re.sub('[^a-z]','',word)
  10.                 if word in toys:
  11.                     cnt[word][0]+=1
  12.                     is_updated[word] = True
  13.             for key in is_updated.keys():
  14.                 if is_updated[key]==True:
  15.                     cnt[key][1] += 1
  16.         return [toy for toy, freq in sorted(cnt.items(), key=lambda item:(item[1][0],item[1][1]), reverse=True)][:topToys]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement