runlength encoding rle is a simple compression algorithm that gets a stream of data as the input and returns a sequence of counts of consecutive data values in a row when decompressed the data will be fully recovered as rle is a lossless data compression gets a stream of data and compresses it under a runlength encoding param input the data to be encoded return the encoded string check if the subsequent character does not match add the count and character reset the count and set the character otherwise increment the counter gets a stream of data and decompresses it under a runlength decoding param input the data to be decoded return the decoded string if not numerical expand it for the decoding add it in the counter gets a stream of data and compresses it under a run length encoding param input the data to be encoded return the encoded string check if the subsequent character does not match add the count and character reset the count and set the character otherwise increment the counter gets a stream of data and decompresses it under a run length decoding param input the data to be decoded return the decoded string if not numerical expand it for the decoding add it in the counter
def encode_rle(input):
    
    if not input: return ''

    encoded_str = ''
    prev_ch = ''
    count = 1

    for ch in input:

        
        if ch != prev_ch:
            
            if prev_ch:
                encoded_str += str(count) + prev_ch
            
            count = 1
            prev_ch = ch
        else:
            
            count += 1
    else:
        return encoded_str + (str(count) + prev_ch)


def decode_rle(input):
    
    decode_str = ''
    count = ''

    for ch in input:
        
        if not ch.isdigit():
            
            decode_str += ch * int(count)
            count = ''
        else:
            
            count += ch
    return decode_str