a guest Feb 23rd, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import re
  2. class Solution():
  3.     def topNBuzzWords(self,numToys, topToys,toys,numQuotes,quotes):
  4.         cnt = { toy: [0,0] for toy in toys}
  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]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand