Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment Specifications You will develop a Python program that allows the user to decompress a string. The program will prompt for a string to decompress.

Assignment Specifications

You will develop a Python program that allows the user to decompress a string.

The program will prompt for a string to decompress. The decompressed string will have proper carriage returns that replace the backslashes in the input string (hint: leave handling of carriage returns until after everything else is working).

The decompression algorithm looks for a left parenthesis "(". Note that the string method find() is useful to locate a specific character, such as a left parenthesis. Then, use find() to locate the next comma and grab the slice in between to extract the substring representing the first number (which, of course, needs to be converted to an integer). Use find() to extract the second number in a similar way. Now that you have the two numbers, work backwards in the string and use slicing to obtain the substring you need to copy. Repeat.

We will make a simplifying assumption: the only parentheses and backslashes in the compressed string are for encoding (i.e. the original string does not contain any parentheses or backslashes). Also, there are no errors in the compressed string so if you find a left parenthesis, you know that there is a corresponding right parenthesis and that there is a comma in between separating two integers. Finally, parentheses are never nested.

Assignment Notes

1. To clarify the project specifications, sample output is provided at the end of this document.

2. Items 1-7 of the Coding Standard will be enforced for this project.

3. A useful pattern when working with strings is to start with an empty string and add characters to itsomething that works nicely here to build the decompressed string. Also, you will find it useful to have a variable (I called it pos) to keep track of the index of the last character in the decompressed stringuseful because that is where you count back from to find the starting point of the replacement string.

4. Slicing is useful in multiple places in this assignment. For example, it is useful for extracting the string to be copied once you have extracted the tuple of encoding numbers.

5. The string find method takes more arguments than the string you are finding; in particular the next argument is a starting point where your search begins. This feature is useful especially with commas because you are only interested in a comma that is between parentheses; in particular you find a comma after a left parenthesis. If we assume that left_paren_index is the index of a left parenthesis that you already found, then my_string.find(,, left_paren_index + 1) will start searching for a comma after the index of the left parenthesis. This second argument is also useful for finding the next left parenthesisyou want to start looking after the previous one.

6. To simplify your program we will assume that parentheses are only used for encoding. That is, if you find a parenthesis, you know that you have an encoding string of the format (first_number,second_number).

7. A useful first test case (from the famous soliloquy in Shakespeares Hamlet) has only one substitution and is short enough to not have any carriage returns (but it does have a comma to trip you up): to be, or not (14,6)

8. There are two different approaches to solving this problem: a. One uses a while statement for the main loop, using find() to look for indices of the pair of numbers within the encoding tuple. One thing to consider is when to stop the loophow do you know when you dont have any left parentheses left? b. Using a for statement as the main loop is possible, but the logic is messier because you need Booleans to keep track of when you are gathering characters for the two numbers in the encoding tuple as well as a Boolean that keeps track of when you are working on extracting numbers so you recognize that comma as separating the numbers.

9. Leave the handling of carriage returns until the end (which is why they dont appear until the fifth test case). Use the following symbolic constant for the backslash character. It is a double backslash because a backslash has a special meaning (for example the used for carriage return). Therefore, Python will interpret the double backslash as a single backslash. BACKSLASH = "\\"

image text in transcribed

image text in transcribed

Sample output Note that testing will be strict character-by-character because spaces count. Input files are provided in the project directory (named starting with "test") Test 1 Input a string to decompress to be or not (14, 6) Decompressed String to be or not to be, Test 2 (Simple variation on Test 1.) Input a string to decompress To be or not to (13,3) Decompressed String To be or not to be Test 3 Input a string to decompress row, (5,5) (5,3) your boat Decompressed String row, row, row your boat Test 4 (This test adds characters between tuples and characters (only a period in this test) at the end.) Input a string to decompress Pease porridge hot, (20, 15) cold, (21,15) in the p (48, 4) Nine days (42, 3) Decompressed String Pease porridge hot, Pease porridge cold, Pease porridge in the pot, Nine days old

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_2

Step: 3

blur-text-image_step3

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

12-5 How will MIS help my career?

Answered: 1 week ago