WordCount

Save all functions into the same file named words.py.

  1. 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.txt

  2. Write 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.txt

  3. Write the function suffix() that takes a string s with a text and another string suf with a shorter text and returns a list with the words in s that finish with suf in 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.txt

    Solution

    You have some solutions in the file words.py.