Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)

image

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

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

Physics

Authors: Alan Giambattista, Betty Richardson, Robert Richardson

2nd edition

77339681, 978-0077339685

More Books

Students also viewed these Programming questions

Question

What are dirty data? How do dirty data arise?

Answered: 1 week ago

Question

Explain the action potential.

Answered: 1 week ago

Question

Recall the role of various endocrine glands.

Answered: 1 week ago

Question

Identify the parts of a neuron and describe the function of each.

Answered: 1 week ago