Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following is to be codded in Python 3.5. Please follow the directions carefully, everything you need should be in the instructions. If you have

The following is to be codded in Python 3.5. Please follow the directions carefully, everything you need should be in the instructions. If you have any questions please ask before answering the question:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Problem 2. Non-interactive Text Editing A non-interactive text editor executes simple editing commands on a text file using a programming interface (i.e. functions or object methods) in some programming language, like Python. All commands take as input, at minimum, the file name as a parameter, then perform their job (reading or modifying the file) and end returning some results. For this problem we assume the text files have the utf-8 encoding (the default in Python) and they are not too large. Therefore, it's OK to read the file entirely to memory, execute the operations required, then overwrite the file with the modified file content, as a string. This is not the only approach, but it's the simpler one and it works for smaller files Here are the editing commands, with the corresponding function signatures: ed read(filename, from 0, to -1): returns as a string the content of the file named filename, with file positions in the half-open range [from, to). If to 1, the content between from and the end of the file will be returned. If parameter to exceeds the file length, then the function rais eption ValueError with a corresponding e message ed find(filename, search str): finds string search str in the file named by filename and returns a list with index positions in the file text where the string search str is located. E.g. it returns (4, 1000 if the string was found at positions 4 and 100. It returns [Jif the string was not found. ed replace search str, replace with, occurrence -1): replaces search str in the file named by filename with string replace with. If occurrence 1, then it replaces ALL occurrence If occurrence -0, then it replaces only the occurrence with index occurrence, where 0 means the first, 1 means the second, etc. If the occurrence argument exceeds the actual occurrence index in the file of that string, the function does not do the replacement. The function returns the number of times the string was replaced ed append(filename, string): appends string to the end of the file. If the file does not exist, a new file is created with the given file name. The function returns the number of characters written to the file ed write filename, pos str coD: for each tuple (position, s) in collection pos str col (e.g. a listO this function writes to the file at position pos the string s. This function overwrites some of the existing file content. If any position parameter is invalid 0) or greater than the file contents size the function does not change the file and raises ValueError with a proper error message. In case of no errors, the function returns the number of strings written to the file. Assume the strings to be written do not overlap ed inser (filename, pos str col): for each tuple (position, s) in collection pos str col (e.g. a list) this function inserts into to the file content the string s at position pos. This function does no overwrite the existing file content, as seen in the examples below. If any position parameters is invalid K 0) or greater than the original file content length, the function does not change the file at all and raises ValueError with a proper error message In case of no errors, the function returns the number of strings inserted to the file Problem 2. Non-interactive Text Editing A non-interactive text editor executes simple editing commands on a text file using a programming interface (i.e. functions or object methods) in some programming language, like Python. All commands take as input, at minimum, the file name as a parameter, then perform their job (reading or modifying the file) and end returning some results. For this problem we assume the text files have the utf-8 encoding (the default in Python) and they are not too large. Therefore, it's OK to read the file entirely to memory, execute the operations required, then overwrite the file with the modified file content, as a string. This is not the only approach, but it's the simpler one and it works for smaller files Here are the editing commands, with the corresponding function signatures: ed read(filename, from 0, to -1): returns as a string the content of the file named filename, with file positions in the half-open range [from, to). If to 1, the content between from and the end of the file will be returned. If parameter to exceeds the file length, then the function rais eption ValueError with a corresponding e message ed find(filename, search str): finds string search str in the file named by filename and returns a list with index positions in the file text where the string search str is located. E.g. it returns (4, 1000 if the string was found at positions 4 and 100. It returns [Jif the string was not found. ed replace search str, replace with, occurrence -1): replaces search str in the file named by filename with string replace with. If occurrence 1, then it replaces ALL occurrence If occurrence -0, then it replaces only the occurrence with index occurrence, where 0 means the first, 1 means the second, etc. If the occurrence argument exceeds the actual occurrence index in the file of that string, the function does not do the replacement. The function returns the number of times the string was replaced ed append(filename, string): appends string to the end of the file. If the file does not exist, a new file is created with the given file name. The function returns the number of characters written to the file ed write filename, pos str coD: for each tuple (position, s) in collection pos str col (e.g. a listO this function writes to the file at position pos the string s. This function overwrites some of the existing file content. If any position parameter is invalid 0) or greater than the file contents size the function does not change the file and raises ValueError with a proper error message. In case of no errors, the function returns the number of strings written to the file. Assume the strings to be written do not overlap ed inser (filename, pos str col): for each tuple (position, s) in collection pos str col (e.g. a list) this function inserts into to the file content the string s at position pos. This function does no overwrite the existing file content, as seen in the examples below. If any position parameters is invalid K 0) or greater than the original file content length, the function does not change the file at all and raises ValueError with a proper error message In case of no errors, the function returns the number of strings inserted to the file

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions

Question

Discuss the various stages of the technical writing process.

Answered: 1 week ago

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago