Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Deliverables in Python There is one deliverable for this assignment hw5.py Make sure the script obeys all the rules in Homework Script Rules Specification The

Deliverables in Python

There is one deliverable for this assignment

  • hw5.py

Make sure the script obeys all the rules in Homework Script Rules

Specification

The script must have 5 functions

  • open_file_read
  • word_set_from_file
  • ordered_word_set_print
  • word_count
  • set_difference

open_file_read

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 succesful 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.

word_set_from_file

This function must have the following header:

def word_set_from_file(filename):

It must read in a file and create a set of the words in that file.

All words added to the set must be lowercase.

It must return that set.

ordered_word_set_print

This function must have the following header:

def ordered_word_set_print(set):

It must print the elements of the set given as a parameter in alphabetical order.

word_count

This function must have the following header:

def word_count(filename):

It must read in a file and count the words in the file.

It must return the word count.

set_difference

This function must have the following header:

def set_difference(set_1, set_2):

It must return the difference between the two sets given to it as parameters

Script for this assignment

Open an a text editor and create the file hw5.py.

You can use the editor built into IDLE or a program like Sublime.

Test Code

Your hw5.py file must contain the following test code at the bottom of the file:

filename_1 = "gettysburg.txt" set_1 = word_set_from_file(filename_1) ordered_word_set_print(set_1) print() print("Words in " + filename_1 + ":" + str(word_count(filename_1))) filename_2 = "gettysburg_hay.txt" set_2 = word_set_from_file(filename_2) print() print("Words in " + filename_1 + " not in " + filename_2) ordered_word_set_print(set_difference(set_1, set_2)) print() print("Words in " + filename_2 + " not in " + filename_1) ordered_word_set_print(set_difference(set_2, set_1)))

For this test code to work, you must copy gettysburg.txt and gettysburg_hay.txt to your machine.

To do this use FileZilla to copy the files from /home/ghoffman/course_files/it117_files into the directory that holds your hw5.py script.

Suggestions

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.

  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. Copy the first line of the test code into the bottom of the file. 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. 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 by inserting a # at the beginning of the line. 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 that prints the lines in the file. 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. Use the split() method on the line to create a list of all the words in the 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 that prints every word in this list. Run the script. You should see
    Four score and ...
    Fix any errors you find.
  7. Remove the print statement. Write a statement that adds each word to the set. Be sure you make the word lowercase. 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. Remove the # from the first commented line of 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. 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

  • Open IDLE
  • Use the Open command in IDLE to open hw5.py
  • Under the Run menu, select Run Module
  • Your output should look something like this
    a above add advanced ... who will work world years Words in gettysburg.txt:272 Words in gettysburg.txt not in gettysburg_hay.txt advanced battle field fought god under Words in gettysburg_hay.txt not in gettysburg.txt battlefield carried upon

Unix Setup

  • Log in to users3.cs.umb.edu You will be in your home directory.
  • Go to your it117 directory
    cd it117
  • Go to your hw directory
    cd hw
  • Create a directory for this exercise
    mkdir hw5
  • Check that the directory was created
    ls

Copy the file to Unix

  • Open FileZilla and connect to users3.cs.umb.edu You will have to connect using your Unix username and password.
  • Copy the file to the to it117/hw/hw5
  • Copy the files gettysburg.txt and gettysburg_hay.txt to hw5
    cp /home/ghoffman/course_files/it117_files/gettysburg.txt . cp /home/ghoffman/course_files/it117_files/gettysburg_hay.txt .

Testing the script on Unix

  • Connect to Use an ssh client.
  • Go to the directory for this exercise
    cd it117/hw/hw5
  • Make this script executable
    chmod 755 hw5.py
  • Run this script
    ./hw5.py
  • You should see something like
    a above add advanced ... who will work world years Words in gettysburg.txt:272 Words in gettysburg.txt not in gettysburg_hay.txt advanced battle field fought god under Words in gettysburg_hay.txt not in gettysburg.txt battlefield carried upon

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions

Question

What is focal length? Explain with a diagram and give an example.

Answered: 1 week ago

Question

What is physics and how does it apply in daily life?

Answered: 1 week ago

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago