Advertisement
padznich

python test case solution

Jul 10th, 2019
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. def count_common_words(text):
  2.     word_counter = {}
  3.     most_populr_word = tuple()  # (word, count)
  4.    
  5.     for word in  text.split():
  6.        
  7.        
  8.         word_counter.setdefault(word, 0)
  9.         word_counter[word] += 1
  10.        
  11.        
  12.         if most_populr_word:
  13.             if word_counter.get(word) > most_populr_word[1]:
  14.                 most_populr_word = (word, word_counter[word])
  15.         else:
  16.             most_populr_word = (word, word_counter[word])
  17.                
  18.     print(sorted(word_counter.items(), key=lambda kv: kv[1], reverse=True))[:11]
  19.     print(most_populr_word[0])
  20.  
  21. count_common_words(text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement