We have a list of numbers, for example, [1,1,1,1,2,2,3,4,5,3,3,3,11,12].
Because we have four 1's, two 2's, etc. The new list would be as followed: [1,4,2,2,3,1,4,1,5,1,3,3,11,1,12,1].
I have the following code, but I am stuck. Can anyone give me advice?

def rlEncode(data):
for i in range(len(data)):
n = data[0]
if n == data[n+1]:
n = n + 1
n = 1

4 Years
Discussion Span
Last Post by Schol-R-LEA

Assuming your goal is Run-Length Encoding, you need to have some way of marking the places where you've applied the encoding, preferably with some character that would never appear in the data stream, or else one that is rarely used. Assuming that your data is always going to be integers, you could choose an arbitrary non-numeric character, such as '\' (backslash, an escape char used in several such encodings). Thus, you would actually encode it something like this:

['\', 1, 4, 2, 2, 3, 5, '\', 3, 3, 11, 12]

Note that with this scheme, you would want to avoid encoding runs of less than three.

Alternately, given the type of data encoded, you could just use the character representations of the numbers themselves, which would save some space and make it feasible to encode all the runs, though this would require additional processing in decoding:


Another approach that occurred to me would be to use tuples for the runs:

[(1,4), (2, 2), 3, 4, 5, (3, 3), 11, 12]

def decompress(comptext):
    plaintext = list()
    for c in comptext:
        if isinstance(c, int):
        elif isinstance(c, tuple):
            for n in range(c[1]):
            pass # TODO: handle the invalid/error case
    return plaintext

Edited by Schol-R-LEA

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.