IT 117: Intermediate Scripting
Homework 5
Due
Sunday, March 2nd 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 gettysburg.txt
and gettysburg_hay.txt from
/home/ghoffman/course_files/it117_files.
Use FileZilla to do this.
Specification
- This script compares the words found in two files
- It reads each file and creates a set of words found
in the file
- Creates difference sets of the words found in one file
but not the other
- Prints the two difference sets
- The script must contain 4 functions
- open_file_read
- word_set_create
- set_difference
- word_set_print
Functions
open_file_read
word_set_create
set_difference
word_set_print
Test Code
- Your script must contain the following statements
- They should appear at the bottom of your script
file_1 = open_file_read("gettysburg.txt")
file_2 = open_file_read("gettysburg_hay.txt")
word_set_1 = word_set_create(file_1)
word_set_2 = word_set_create(file_2)
set_1_set_2_difference = set_difference(word_set_1, word_set_2)
set_2_set_1_difference = set_difference(word_set_2, word_set_1)
print()
print('Words in the first text not found in the second')
word_set_print(set_1_set_2_difference)
print()
print('Words in the second text not found in the first')
word_set_print(set_2_set_1_difference)
- This code will only work if you have copied
ettysburg.txt and
gettysburg_hay.txt into the same directory
as your script
- You will find these files in
/home/ghoffman/course_files/it116_files
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 a file with the hashbang line.
Copy and paste the headers for each of the three functions
into the scrpt.
The code block for each of these functions should be the single
Python statement pass
.
Pass does nothing but it will keep the code from giving syntax errors.
Run the code.
You should see nothing.
If you have errors, fix them.
-
Copy the definition of open_file_read from
hw4.py into your script.
Copy the test code to the bottom of your script.
Place a # character before each line of the test script
except the first two lines.
This will stop these statements from running for now.
This is called "commenting out the code.
Run the script and fix any errors you find.
-
Remove the
pass
statement from
word_set_create.
Create an empty set called word_set.
Print word_set.
Run the script and fix any errors you find.
-
Remove the print statement above.
Replace it with a for
loop that prints each line in the file.
Run the script and fix any errors you find.
-
Before the
print
statement, write an assignment statement
that turns line into lowercase using the
lower
string method.
Run the script and fix any errors you find.
-
Remove the
print
method and replace it with an assignment
statement that creates the variable word_list
by running the split()
method on line.
Print word_list.
Run the script and fix any errors you find.
-
Remove the previous print statement.
In its place write a for
loop that prints each
word in word_list.
Run the script and fix any errors you find.
-
Replace the print statement with a statement that adds
word to word_set.
Outside both for
loops, print word_set.
Run the script and fix any errors you find.
-
Remove the final print statement.
Replace it with a statement that returns word_set.
Remove the pass
statement from set_difference.
Replace it with a statement that prints the value of the parameters
set_1 and set_2.
Run the script and fix any errors you find.
-
Remove the
print
statement.
In it's place write an assignment statement that sets the difference between
set_1 and set_2 to the
variable difference.
Print difference.
Run the script and fix any errors you find.
-
Replace the
print
statement above with a statement that
returns difference.
Replace the the pass
statement in
word_set_print with a statement
that prints the parameter word_set.
Remove the # from the test code.
Run the script and fix any errors you find.
-
Replace the
print
statement in
word_set_print with
a for
loop that prints each element of the set.
Run the script and fix any errors you find.
-
Change the
for
loop so it prints the words in alphabetical order.
Run the script and fix any errors you find.
Output
Your output should look something like this:
Cannot open file xxxxxxx
Words in the first text not found in the second
advanced
battle
field
fought
god
under
Words in the second text not found in the first
battlefield
carried
upon
Copy the file to Unix
- Open FileZilla and connect to
pe15.cs.umb.edu
You will have to connect using your Unix username and password.
- Go to your it117 directory
- Go to your hw directory
- Create a directory for this exercise
Right-click in the whitespace and create the hw5
directory
- Copy the script to the to
hw5
Click and drag the file from the bottom left panel to the bottom right panel
- Make the script executable
Right-click on the file and select "Permissions" from the menu
Enter 755 in the box provided
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 gettysburg.txt
and gettysburg_hay.txt from the bottom
left panel to the bottom right panel
Testing the Script on Unix (Optional)
Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.