RESOURCES

COMP150: Practice Exercises

Progression 5: Practice exercises


Download the files you need for these exercises: Progression 5 support files

Don't alter the answer files included in the support files above - they are needed for the doctests in questions 3, 4 and 5 to work.

Take a good look at the doctests to further understand the question requirements.

Question 1

Write a function that takes a single string representing a filename as an argument.

The function should open the file and return the number of letters (not digits or other characters, only upper and lower case letters) that it contains.

Remember to have all the required files in the same directory as the python file.

Your function should pass the doctests below:

def letter_count(file_name):
    """
    >>> letter_count("russia.txt")
    3109
    >>> letter_count("antigua.txt")
    2974
    """

def letter_count(file_name):
    """
    >>> letter_count("russia.txt")
    3109
    >>> letter_count("antigua.txt")
    2974
    """
    file_in = open(file_name)
    text = file_in.read()

    count = 0
    for ch in text:

        if ch.isalpha():
            count+=1
    file_in.close()# not always necessary but good practice for now
    return count

Question 2

Write a function called count_possible_sentences that takes the name of a file as an argument and returns how many words end in '!', '?' or '.'.

Remember to have all the required files in the same directory as the python file.

Your function should pass the doctests below:

           
def count_possible_sentences(file_name):
    """
    >>> count_possible_sentences("russia.txt")
    22
    >>> count_possible_sentences("antigua.txt")
    27
    >>> count_possible_sentences("test.txt")
    1
    """

def count_possible_sentences(file_name):
    """
    >>> count_possible_sentences("russia.txt")
    22
    >>> count_possible_sentences("antigua.txt")
    27
    >>> count_possible_sentences("test.txt")
    1
    """
    file_in = open(file_name)
    text = file_in.read()

    
    words = text.split()
    count = 0
    for word in words:

        if word[-1] in "!?.":
            count+=1
    file_in.close()          
            

    return count

Question 3

Write a function called capitalise_words that takes two file names. The first file name is the file to be read and processed, the second is the destination file for your answer.

The function should open the given file for processing and then write out to the destination file. In the destination file each word from the given file should be capitalised (i.e. the first letter should be a capital and the rest should be lower case) and followed by a space. You may make use of the capitalize method from str.

Note: You do not have to strip punctuation. Commas and full stops etc are just treated as a part of the word.

Remember to have all the required files in the same directory as the python file.

Your function should pass the doctests below:

def capitalise_words(file_name, destination):
    """
    >>> capitalise_words("russia.txt", "russia_capitalised.txt")
    >>> f1 = open("russia_capitalised.txt")
    >>> text1 = f1.read()
    >>> f2 = open("russia_answer_capitalised.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    >>> capitalise_words("antigua.txt", "antigua_capitalised.txt")
    >>> f1 = open("antigua_capitalised.txt")
    >>> text1 = f1.read()
    >>> f2 = open("antigua_answer_capitalised.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    """

def capitalise_words(file_name, destination):
    """
    >>> capitalise_words("russia.txt", "russia_capitalised.txt")
    >>> f1 = open("russia_capitalised.txt")
    >>> text1 = f1.read()
    >>> f2 = open("russia_answer_capitalised.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    >>> capitalise_words("antigua.txt", "antigua_capitalised.txt")
    >>> f1 = open("antigua_capitalised.txt")
    >>> text1 = f1.read()
    >>> f2 = open("antigua_answer_capitalised.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    """
    file_in = open(file_name)
    file_out = open(destination, 'w')
   

    text = file_in.read()
    words = text.split()            
            

    for word in words:
        file_out.write(word.capitalize() + ' ')            
            

    file_in.close()
    file_out.close()            
            

Question 4

Write a function called strip_punctuation that takes two file names. The first file name is the file to be read and processed, the second is the destination file for your answer.

The function should open the given file for processing and then write out to the destination file. In the destination file any character that is whitespace should be replaced by a single space. Any other character that is not a letter or a digit should be removed.

Your function should pass the doctests below:

def strip_punctuation(file_name, destination):
    """
    >>> strip_punctuation("russia.txt", "russia_no_punc.txt")
    >>> f1 = open("russia_no_punc.txt")
    >>> text1 = f1.read()
    >>> f2 = open("russia_answer_no_punc.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    >>> strip_punctuation("antigua.txt", "antigua_no_punc.txt")
    >>> f1 = open("antigua_no_punc.txt")
    >>> text1 = f1.read()
    >>> f2 = open("antigua_answer_no_punc.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    """
            

def strip_punctuation(file_name, destination):
    """
    >>> strip_punctuation("russia.txt", "russia_no_punc.txt")
    >>> f1 = open("russia_no_punc.txt")
    >>> text1 = f1.read()
    >>> f2 = open("russia_answer_no_punc.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    >>> strip_punctuation("antigua.txt", "antigua_no_punc.txt")
    >>> f1 = open("antigua_no_punc.txt")
    >>> text1 = f1.read()
    >>> f2 = open("antigua_answer_no_punc.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    """
    file_in = open(file_name)
    file_out = open(destination, 'w')

    text = file_in.read()
    for ch in text:

        if ch.isspace():
            file_out.write(' ')

        elif ch.isalpha() or ch.isdigit():
            file_out.write(ch)

   
    file_in.close() 
    file_out.close()

Question 5

Write a function called save_word_lengths that takes two file names. The first file name is the file to be read and processed, the second is the destination file for your answer.

The function should open the given file for processing and then write out to the destination file. In the destination file you should write the length of each word from the source file followed by a space.

Your function should pass the doctests below:

def save_word_lengths(file_name, destination):
    """
    >>> save_word_lengths("russia.txt", "russia_word_lengths.txt")
    >>> f1 = open("russia_word_lengths.txt")
    >>> text1 = f1.read()
    >>> f2 = open("russia_answer_word_lengths.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    >>> save_word_lengths("antigua.txt", "antigua_word_lengths.txt")
    >>> f1 = open("antigua_word_lengths.txt")
    >>> text1 = f1.read()
    >>> f2 = open("antigua_answer_word_lengths.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    """

def save_word_lengths(file_name, destination):
    """
    >>> save_word_lengths("russia.txt", "russia_word_lengths.txt")
    >>> f1 = open("russia_word_lengths.txt")
    >>> text1 = f1.read()
    >>> f2 = open("russia_answer_word_lengths.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    >>> save_word_lengths("antigua.txt", "antigua_word_lengths.txt")
    >>> f1 = open("antigua_word_lengths.txt")
    >>> text1 = f1.read()
    >>> f2 = open("antigua_answer_word_lengths.txt")
    >>> text2 = f2.read()
    >>> text1 == text2
    True
    """
    
    file_in = open(file_name)
    file_out = open(destination, 'w')
    

    text = file_in.read()
    words = text.split()
    for word in words:

        file_out.write(str(len(word)) + ' ')
        
    file_in.close()
    file_out.close()