Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this problem, you will be implementing a basic data compression algorithm. Wr

ID: 3712472 • Letter: I

Question

In this problem, you will be implementing a basic data compression algorithm. Write a function compress.line ), which takes one argument, line, a string that you will need to compress it. The algorithm works like this: take a string and start reading from the beginning, compress identical consecutive characters in the form of count of that character directly followed by that character. In the end, the original string will be shortened (hopefully!), and the function should return that shortened result. For example, the string ' AAABBBCCC' will be shortened to '3A3B3C'; the string 'AAC!?????' will be

Explanation / Answer

# cook your code here
import string
def compress_line(st):
    out = ""
    i = 0
    while i<len(st)-1:
        count = 1
        ch = st[i]
        i += 1
        if ch==' ':
            continue
        while ch == st[i]:
            i += 1
            count += 1
            if i == len(st):
                break
        out += str(count)+ch
    return out
print compress_line(" PPPPKKKLL gggf+ ")

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote