Most common word
Most common word
Find the favourite word of a piece of text.
Work in three stages. Clean: remove the punctuation with .replace() and make everything lowercase with .lower(). Count: .split() the text into words and count them in a dictionary with counts.get(word, 0) + 1. Choose: walk through the words in their original order and switch to a word only when its count is strictly bigger, so that a tie goes to the earlier word.
Difficulty 5/5 · 400 coins + a small bonus on first completion today
Write most_common_word(text) that returns the word used most often, ignoring upper and lower case and the punctuation marks .,!?. If two words tie, return the one that appears first.
Click Run to see the output here.