Question: PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters.
PLEASE CODE IN PYTHON
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?
Write the function def rle(string, flag) 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 {#, $, &, *}, 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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
