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
else:
n = 1
print(n)

## All 2 Replies

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,1,1,1,2,2,3,4,5,3,3,3,11,12]
['\', 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:

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

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):
plaintext.append(c)
elif isinstance(c, tuple):
for n in range(c[1]):
plaintext.append(c[0])
else:
pass # TODO: handle the invalid/error case
return plaintext
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.