Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Pyhton Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA

Pyhton

Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA is a run of 10 As. We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run. As another example, the string

KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG 

would be encoded

$K13BCC$D15$K5MNUUU$G5 

assuming in this case that $ is the flag character. For the sake of this problem we will assume that the input strings to be encoded contain only uppercase letters from the Latin alphabet and that no run is longer than 99 characters long. Flag characters will be chosen only from the set {#, $, &, *}. Note that single letters (M), runs of two letters (CC), and runs of three letters (UUU) are not encoded, as doing so does not save memory or actually compress the data. Do you see why that is the case?

Now write the function rld() that performs a run-length decoding of a string that was encoded using the rle() function described above. The functions arguments are a non-empty string to decode and the character to use as the flag character. The input string consists of a properly-formatted run-length encoding, and the flag character is drawn from the set {#, $, &, *}. You may assume that the input string and the flag characterare both valid.

image text in transcribed

def rle(string, flag): count = 0 prev = string[0] result = ''  index = 0 while index if not string[index].isalpha() or string[index].islower() or flag not in '#$&*': return "ERROR"  if prev == string[index]: count += 1 else: if count > 3: result += flag + prev + str(count) else: result += prev * count count = 1 prev = string[index] index += 1 if index == len(string): # check index is equal to string length  if count > 3: result += flag + prev + str(count) else: result += prev * count return result def rld(encoded, flag): #write your code here return None 

if __name__ == "__main__": 
 encoded1 = 'XYZ*A6GGT*C6TTT*A14KK'  flag_char1 = '*'  print('Testing rld() for "' + encoded1 + '", "' + flag_char1 + '": ' + str(rld(encoded1, flag_char1))) encoded2 = '#G5'  flag_char2 = '#'  print('Testing rld() for "' + encoded2 + '", "' + flag_char2 + '": ' + str(rld(encoded2, flag_char2))) encoded3 = '&G15ABC'  flag_char3 = '&'  print('Testing rld() for "' + encoded3 + '", "' + flag_char3 + '": ' + str(rld(encoded3, flag_char3))) encoded4 = 'ABCCDEFGGG'  flag_char4 = '$'  print('Testing rld() for "' + encoded4 + '", "' + flag_char4 + '": ' + str(rld(encoded4, flag_char4))) 
Examples: Return Value Function Call rld ('XYZ* A6GGT C6TTT Al 4KK' XYZAAAAAAGGTCCCCCCT TTAAAAAAAAAAAAAAKK' rld G57, GGGGG' rld ('&G15ABC', &') GGGGGGGGGGGGGGGABC' rld ABCCDEF GGG', ABCCDEF GGG' Note that the quotation marks displayed in the return values are there to emphasize that the return values are strings. You should not add quotation marks to your return values

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

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions