Question: how can I solve this problem?? So far, I got this code def rle(string, flag): count = 1 prev = '' result = '' for

 how can I solve this problem?? So far, I got this

how can I solve this problem?? So far, I got this code

def rle(string, flag): count = 1 prev = ''  result = ''   for character in string: if character != prev: if prev: entry = flag + prev + str(count) result += entry count = 1 prev = character else: count += 1 else: entry = flag + character + str(count) result += entry return result 

Part I: Run-length Encoding (5 points) 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 KKKKKKKKKKKKK BCC DDDDDDDD DDDDDDDKKKKKIMNUUUGGGGG would be encoded SK13BCCSD 1.5 $K5MNUUU$S 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 t# &, Y. 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? Write the function rle that takes a non-empty string to encode and a character to use as the flag character The function returns the run-length encoded argument string. If the string to encode contains any characters except uppercase letters, the function should return the string ERROR' without the quotation marks). If the flag character is not one of the symbols in the set t#, &, Y, the function should return the string ERROR Consider using a while-loop instead of a for-loop to iterate over the string. Start with an empty string (which will eventually contain the returned result) and keep a counter for the number of identical characters you find in each run. When a run ends, append (i the flag character, (ii) the count, and (iii the character itself to the result. Remember that only runs of length 4 or greater should be encoded; single characters, pairs and triples should simply be appended to the result. Examples: Function Call Return Value rle GGGGG' #G5' rle ('XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAAAKK', 'XYZKA6GGTKC6TTT *A14 KK' rle ('ABCCDEF GGG' ABCCDEF GGG rle ABCCDEF GGGG', ABCCDEFSG4' rle ('XYZAAAAAAA GGTCCCCC CTTTa aAAAAAAAAAAAAAAKK' ERROR rle ('XYZAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAAAKK' ERROR

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