WordCount¶
Save all functions into the same file named words.py.
Write the function
word_count()that takes a string with a text and returns the number of words in that text. The function must pass the following doctest:>>> word_count("He's five feet two and he's six feet four...") 9 >>> word_count("He fights with missiles and with spears...") 7 >>> word_count("He's been a soldier for a thousand years...") 8
Note
You can download a file with tests
words.txtWrite the function
initials()that takes a non-empty string with a text and another string with a single character that is a lowercase letter and returns the percentage of words in that text beginning with the given letter. The function must pass the following doctest:>>> round(initials('This land is your land, this land is my land...', 'l'), 2) 40.0 >>> round(initials('This land is your land, this land is my land...', 't'), 2) 20.0 >>> round(initials("Hello darkness, my old friend, I come to talk with you again...", 't'), 2) 16.67 >>> round(initials('Hey mister Tambourine man, play a song for me...', 'm'), 2) 33.33
Note
You can download a file with tests
initials.txtWrite the function
suffix()that takes a stringswith a text and another stringsufwith a shorter text and returns a list with the words insthat finish withsufin alphabetical order and without repetitions. We can assume that there are no punctuation marks in the text. Examples:>>> suffix('So you think you can tell heaven from hell', 'ell') ['hell', 'tell'] >>> suffix('But they sent me away to teach me how to be sensible logical responsible practical and they showed me a world where I could be so dependable clinical intellectual cynical', 'al') ['clinical', 'cynical', 'intellectual', 'logical', 'practical'] >>> suffix('But they sent me away to teach me how to be sensible logical responsible practical and they showed me a world where I could be so dependable clinical intellectual cynical', 'ble') ['dependable', 'responsible', 'sensible'] >>> suffix('The answer my friend is blowing in the wind', 'ing') ['blowing']
Note
You can download a file with tests
suffix.txtSolution
You have some solutions in the file
words.py.