question 106 String Algorithms · medium
The word counts are split and repeated
word_counts(text) should return a list of (word, count) pairs, one per distinct word, in the order each word first appears. Words are compared in lower case with punctuation .,!?;: stripped from their ends. On a complaint it reports Fee and fee, as different words, and lists fee once for every time it appears. Fix it.
Function
word_counts(text)Returnsa new list of
(word, count) tuples, distinct words in first-seen orderWordssplit on whitespace, lowered, with
.,!?;: stripped from the ends; words that become empty are ignoredExamples
callword_counts('Fee charged. Another fee, fee waived?')returns[('fee', 3), ('charged', 1), ('another', 1), ('waived', 1)]
callword_counts('late late LATE')returns[('late', 3)]
callword_counts('')returns[]