IT 117: Introduction to Scripting
Homework 5
Due
Sunday, October 5th at 11:59 PM
What You Need to Do
- Create the script hw5.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Make sure the script has a hashbang line and is executable
- Move it to an an hw5
directory on pe15.cs.umb.edu
Setup On Your Machine
- Open a text editor.
I would suggest the text editor built into the program IDLE
.
- Save the file as hw5.py
- Copy the files words_1.txt
and words_2.txt from
/home/ghoffman/course_files/it117_files.
Use FileZilla to do this.
Specification
- This script reads two text files and converts them into sets
- Each file has a single word on each line
- It then prints the lengths of the two sets and combinations
of these sets
- Then it prints the intersection of the two sets
- The script must contain 3 functions
- open_file_read
- set_from_file
- print_sorted_set
Functions
open_file_read
set_from_file
- Header
def set_from_file(file):
- The function should loop through a text file with one
word on each line
- It should add all words to a set and return the set
print_sorted_set
- Header
def print_sorted_set(s):
- This function should print all entries in a set in
sorted order
Test Code
- Your script must contain the following statements
- They should appear at the bottom of your script
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
- Write this script in stages
- Test your script at each step
- Print the steps below
- And check them off as you finish each one
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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
- Open FileZilla and connect to
pe15.cs.umb.edu
- Go to your it117 directory
- Go to your hw directory
- Right-click in the whitespace inside the
hw directory
- Enter hw5 in the dialog box
- Click and drag your script from the bottom left panel
to the bottom right panel
- Right-click on the file and select "Permissions" from
the menu
- Enter 755 in the box provided
- This will make the script executable
- Click and drag words_1.txt
and words_2.txtfrom the
bottom left panel to the bottom right panel
Testing the Script on Unix (Optional)
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.