There is one deliverable for this assignment
The script must have 4 functions:
This function must have the following header
def get_filenames():
The function should check that number of command line arguments.
If there are fewer than 2 command line arguments, it should print an error message and exit the script.
If there are at least 2 command line arguments it should return the first two command line arguments.
This function must have the following header
def open_file_read(filename):
It must try to create a file object for reading on the file whose name is given by the parameter filename.
If it is succesfull in creating the file object it should return the object.
If it is cannot create the object it should print an error message
and return None
.
This function must have the following 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.
This function must have the following header
def print_ordered_set(s):
This function should print all entries in a set in sorted order.
Open an a text editor and create the file hw5.py.
You can use the editor built into IDLE or a program like Sublime.
Your hw5.py file must contain the following test code at the bottom of the file
filename_1, filename_2 = get_filenames() print(filename_1, filename_2) file_1 = open_file_read(filename_1) file_2 = open_file_read(filename_2) if file_1 and file_2: set_1 = set_from_file(file_1) set_2 = set_from_file(file_2) print(len(set_1)) print(len(set_2)) print(len(set_1 | set_2)) print(len(set_1 & set_2)) print_ordered_set(set_1 & set_2)
Write this program in a step-by-step fashion using the technique of incremental development.
In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.
pass
. pass
statement in get_filenames
with an if
statement that checks that the number of
command line arguments is at least two. pass
statement from
set_from_file. if
statement, to the bottom of the script. print
statement from
set_from_file. for
loop that prints each line in the file. print
statement after the for
loop in
set_from_file. for
loop, print the set s. print
statement from the last line of
set_from_file. pass
statement from
print_ordered_set . print
statement from
print_ordered_set. for
loop that prints the sorted list
of elements in the set. python hw5.py words_1.txt words_2.txt
21 27 39 9 have in is largely learned may remote that the
cd it117
cd hw
mkdir hw5
ls
cp /home/ghoffman/course_files/it117_files/words_1.txt . cp /home/ghoffman/course_files/it117_files/words_2.txt .
cd it117/hw/hw5
chmod 755 hw5.py
./hw5.py
words_1.txt words_2.txt 21 27 39 9 have in is largely learned may remote that the
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.