IT 117: Introduction to Scripting
Homework 5

Due

Sunday, October 6th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read


count_words


word_set_create


different_words


common_words


Test Code

Output

Suggestions

  1. Create the file hw5.py.
    Enter the headers for each of the required functions.
    Under each header write the Python statement pass.
    Run the script.
    Fix any errors you find.
  2. Replace the pass statement in open_file_read with the body of the code from your hw4.py script.
    Copy the test code to the bottom of the script.
    Write a # as the first character in each line, except the first 4 lines.
    By doing this you have "commented out" all but the first 4 lines of the test code.
    This means only the first 4 lines of the test code will run.
    Run the script.
    Fix any errors you find.
  3. Replace the pass statement in count_words with a statement assigns 0 to the variable count.
    Now write a for loop that loops over the lines in the file.
    Inside the loop print each line.
    Uncomment the 5 lines in the test code starting with
    count_1 = count_words(file_1)
    Run the script.
    You should not see any output.
    Fix any errors you find.
  4. Remove the print statement.
    Create the list word_list by running split on each line.
    Print word_list.
    Run the script.
    Fix any errors you find.
  5. Remove the printstatement.
    Replace it with a statement that increments count by the length of the list word_list.
    Outside the for loop return count.
    Run the script.
    You should see
    272
    268
    Fix any errors you find.
  6. Remove the pass statement from word_set_create.
    Replace it with a statement that creates the empty set word_set.
    Now write a for loop that loops over the lines in the file.
    Inside the loop print each line.
    Uncomment the 4 lines in the test code beginning with
    file_1     = open_file_read(filename_1)
    Run the script.
    Fix any errors you find.
  7. Remove the print statement.
    Create the list word_list by running split on each line.
    Print word_list.
    Run the script.
    Fix any errors you find.
  8. Remove the print statement.
    In its place write another for loop that loops over the words in word_list.
    word would be a good name for the loop variable.
    Inside this second loop, print each word in word_list.
    Run the script.
    Fix any errors you find.
  9. Remove the print statement.
    Replace it with a statement that adds the lowercase value of each word to word_set.
    Outside both loops, return word_set.
    Uncomment the 5 lines in the test code beginning with
    print("Filename            Words  Unique Words")
    Run the script.
    You should see
    Filename            Words  Unique Words
    ---------------------------------------
    gettysburg.txt      272    132
    gettysburg_hay.txt  268    135
    Fix any errors you find.
  10. Remove the pass statement from different_words.
    There is a set method which finds all the elements that are in set_1 but not set_2 as well as all elements that are in set_2 but not set_1.
    Write a return statement which calls this method on the parameters set_1 and set_2.
    Uncomment the 5 lines in the test code beginning with
    different_word_set = different_words(word_set_1, word_set_2)
    Run the script.
    You should see
    The two files have 9 words in one file, but not the other
    advanced
    battle
    battlefield
    carried
    field
    fought
    god
    under
    upon
    Fix any errors you find.
  11. Remove the pass statement from common_words.
    There is a set method what will return all the elements found in both sets.
    Write a return statement which calls this method on the parameters set_1 and set_2.
    Uncomment the last two lines in the test code.
    Run the script.
    You should see
    The two files have  132  words in common
    Fix any errors you find.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

Copyright © 2024 Glenn Hoffman. All rights reserved. May not be reproduced without permission.