Question: Python problem developing a program allowing user to decompress a string. The decompressed string must have proper carriage returns that replace the backslashes in the

Python problem developing a program allowing user to decompress a string. The decompressed string must have proper carriage returns that replace the backslashes in the input string. Use find() to locate the next comma and grab the slice in between to extract the substring representing the first number. Use find() to extract the second number in a similar way. Now having the two numbers, work backwards in the string and use slicing to obtain the substring you need to copy. Repeat.

assumptions:

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.

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.

To simplify the program 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).

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)

2 approaches -> for and while to be used only

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 = "\\"

Sample Output

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 (This test adds back-to-back tuples.) 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.

Test 5 (This test adds carriage returns.) Input a string to decompress: The Rain\Pitter pa(7,4)\(14,14)Lis(7,2)n to t(47,2) r(47,4)(47,28)On (40,3) window (21,2)ne

Decompressed String

The Rain

Pitter patter

Pitter patter

Listen to the rain

Pitter patter

Pitter patter

On the window pane

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!