IT 117: Introduction to Scripting
Homework 5

Due

Sunday, March 1st at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read


word_set_from_file

	create an empty set
	for each line in the file:
	   create the list of words by running split() on line
	   for each word in the word list:
	      make the word lowercase and add it to the set
	return the set

ordered_word_set_print


word_count

	create a file object for reading
	set count equal to 0
	for each line in the file:
		create list of the words in the file using split()
		add the length of the list to count
	return count
	

set_difference

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.
    Run the script.
    Fix any errors you find.
  3. Replace the pass statement in word_set_from_file with a statement that creates the empty set words_set.
    Write a statement that creates a file object by calling open_file_read using the filename parameter.
    Copy the test code to the bottom of the file.
    Comment out all but the first two lines of the test code.
    Run the script.
    You should not see any output.
    Fix any errors you find.
  4. After the statement that creates a file object, write a for loop with the loop variable line
    Inside the for loop print line.
    Run the script.
    You should see something like this
    Four score and seven years ago our fathers brought forth on this
    
    continent a new nation conceived in Liberty and dedicated to the
    
    proposition that all men are created equal
    
    ...
    Fix any errors you find.
  5. Remove the print statement.
    Create the list words by running the split() method on line.
    Print this list.
    Run the script.
    You should see
    ['Four', 'score', 'and', 'seven', 'years', 'ago', 'our', 'fathers', 'brought', 'forth', 'on', 'this']
    ['continent', 'a', 'new', 'nation', 'conceived', 'in', 'Liberty', 'and', 'dedicated', 'to', 'the']
    ...
    Fix any errors you find.
  6. Remove the print statement.
    Write a for loop using the loop variable word.
    Inside the loop print each word.
    Run the script.
    You should see
    Four
    score
    and
    ...
    Fix any errors you find.
  7. Remove the print statement.
    Make each word lowercase using the string method lower.
    You have to do this with an assignment statement because strings cannot be changed in Python.

    Write a statement that adds the word to the set.
    Outside both for loops print the set.
    Run the script.
    You should see something like this
    {'on', 'fathers', 'conceived', 'nation', 'but', 'god', ...
    Fix any errors you find.
  8. Remove the print statement from word_set_from_file.
    Write a statement that returns the set.
    Remove the pass statement from ordered_word_set_print.
    Write a statement that prints the argument set.
    Uncomment the next line in the test code.
    Run the script.
    You should see something like this
    {'new', 'above', 'whether', 'under', 'or', ...
    Fix any errors you find.
  9. Remove the print statement from ordered_word_set_print.
    Write a for loop that prints each word in the set.
    Make sure it prints them in alphabetical order.
    Run the script.
    You should see
    a
    above
    add
    advanced
    
    Fix any errors you find.
  10. Remove the pass statement from word_count.
    Write a statement that sets the variable count to 0.
    Write a statement that creates a file object by calling open_file_read.
    Uncomment the next two lines in the test code.
    Run the script.
    You should only see the words in the set you created above.
    Fix any errors you find.
  11. Write a for loop that prints each line in the file.
    Run the script.
    You should see the lines of the file at the bottom of your output but no new output.
    Fix any errors you find.
  12. Remove the print statement in the for loop.
    Create a list of all the words in the line using split() on the line.
    Add the length of this list to count.
    Outside the for loop print count.
    Run the script.
    At the bottom of the output you should see
    272
    Words in gettysburg.txt:None
    Fix any errors you find.
  13. Remove the print statement.
    Replace it with a statement that returns count. Run the script.
    At the bottom of the output you should see
    Words in gettysburg.txt:272
    Fix any errors you find.
  14. Remove the pass statement in set_difference.
    Replace it with a statement the returns the difference between the two parameters, set_1 and set_2.
    Uncomment the remaining lines in the test code.
    Run the script.
    Fix any errors you find.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

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