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

need help with this python assignment Part I: Run-length Encoding (5 points) Run

ID: 3817046 • Letter: N

Question

need help with this python assignment

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 A's. 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 KKKKKKKKKKKKKBCO2DDDDDDDDD DDDDDDKKKKKMNUUUGGGGG ould be encoded 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#, & 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#, &, 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' rle 'XYZAAAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAAAAKK' XYZ *A6GGT kC6TTT* Al 4 KK' T$ rle ABCCDEF GGG' ABCCD EEGGG ABC CDEFSG4' rle ('ABCC DEFGGGG', rle 'XYZAAAAAAAGGTCCCCCCTTTaAAAAAAAAAAAAAAAKK' ERROR' D rle XYZAAAAAAAAAGGTCCCCCCTTTAAAAAAAAAAAAAAAAAAAAAKK' ERROR' Note that the quotation marks displayed in the return values are there to emphasize that the return values are trings. You should not add quotation marks to your return values

Explanation / Answer

# linkf ro code in case indentation mess up: https://goo.gl/Tm7HZY

def rle(string, flag):
count = 0
prev = string[0]
result = ''
index = 0
while index < len(string):
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):
result = []
length = len(encoded)
i = 0
while i < length:
if encoded[i] == flag:
i += 1
char = encoded[i]
i += 1
digit = encoded[i]
  
if (i+1) < length and encoded[i+1].isdigit():
digit = digit + encoded[i+1]
i += 1
digit = int(digit)
for x in range(0, digit):
result.append(char)
else:
result.append(encoded[i])
i += 1
return "".join(result)

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)))