IT 117: Introduction to Scripting
Homework 5

Due

Sunday, October 5th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

open_file_read


set_from_file


print_sorted_set

Test Code

file_1 = open_file_read("words_1.txt")
file_2 = open_file_read("words_2.txt")
if file_1 and file_2:
    set_1 = set_from_file(file_1)
    set_2 = set_from_file(file_2)
    print("set 1:", len(set_1), "elements")
    print("set 2:", len(set_2), "elements")
    print("Union sets 1 & 2:",len(set_1 | set_2), "elements")
    print("Intersection sets 1 & 2:",len(set_1 & set_2), "elements")
    print("Elements in intersection sets 1 & 2")
    print_sorted_set(set_1 & set_2)

Output

Suggestions

  1. Create the file hw5.py.
    Enter the headers for open_file_read, set_from_file and print_sorted_set.
    Under each header write the Python statement pass.
    Run the script.
    Fix any errors you find.
  2. Copy the function open_file_read from your hw2.py.
    Copy the test code into the bottom of the file.
    Comment out all but the first two lines of the test code by placing the hash mark, #, at the beginning of each line.
    Run the script.
    You should see nothing.
    Fix any errors you find.
  3. Remove the pass statement from set_from_file.
    Write a statement that defines the empty set s.
    Print s.
    Remove the comment from the next three lines in the test code, the lines starting with the if statement.
    Run the script.
    Fix any errors you find.
  4. Remove the print statement from set_from_file.
    Write a for loop that prints each line in the file.
    Run the script.
    Fix any errors you find.
  5. Remove the print statement after the for loop in set_from_file.
    Replace it with a statement adds the line, with the linefeed character removed, to the set s.
    Outside the for loop, print the set s.
    Run the script.
    Fix any errors you find.
  6. Remove the print statement from the last line of set_from_file.
    Replace it with a line that returns the set s.
    Remove the pass statement from print_sorted_set .
    Replace it with a statement that prints the parameter s.
    Uncomment the remaining lines in the test code.
    Run the script.
    Fix any errors you find.
  7. Remove the print statement from print_sorted_set.
    Replace it with a for loop that prints the sorted list of elements in the set.
    Run the script.
    Fix any errors you find.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

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