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 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started