Question: For this task, you are to implement a simple compression algorithm. The logic for this particular simple compression algorithm is shown in the flowchart below:

For this task, you are to implement a simple compression algorithm.

The logic for this particular simple compression algorithm is shown in the flowchart below: The purpose of the algorithm is to represent text in a way that is more efficient for many repeated characters. See the examples for how this is expected to work.

Using the provided code as a starting point, complete the program so that the behaviour of your code matches the flowchart. The code below is to here to help ans start the process:

# TODO: Fix this code so that it matches the flowchart

inp = input('Text to compress: ')

compressed = '' count = 1 prev_char = inp[0] for char in inp[1:]: if char == prev_char: count = count + 1 compressed = compressed + str(count) + prev_char count = 1

print(compressed)

Start Get input text from the user Let 'compressed be an empty

Start Get input text from the user Let 'compressed be an empty string and start the count at 1 Let `prev_char' be the first character from the input text Are there more characters in the input text? Yes Let `char be the next character from the input text Is 'char' the same as `prev_char'? Yes Add 1 to the count Update 'prev_char' to 'char Add the count and prev_char to the end of compressed Display compressed Stop No No Add the count and 'prev_char to the end of compressed* Reset the count to 1

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 Programming Questions!