@darktrym: Das Folgende sollte die optimale Lösung finden. Benutzt dynamische Programmierung. Rechnet von hinten, und kennt daher für alle Suffixe bereits die optimale Lösung.
Code: Alles auswählen
def RLE(A):
optimal = [None] * len(A)
optimal.append((0, "dummy", 0, [])) # total length of encoded sequence, runtype (0 or 128), number of values, list of values
for start in range(len(A))[::-1]:
candidates = []
# non-identical runs
for run_length in range(min(len(A) - start, 127)):
candidates.append((2 + run_length + optimal[start + run_length + 1][0], 128, run_length + 1, A[start:start+run_length+1]))
# identical runs
for run_length in range(1, min(len(A) - start, 127)):
if A[start] != A[start + run_length]:
break
candidates.append((2 + optimal[start + run_length + 1][0], 0, run_length + 1, A[start:start+1]))
optimal[start] = min(candidates)
steps = []
pos = 0
while pos < len(A):
length, run_type, run_length, values = optimal[pos]
steps.append(run_type + run_length)
steps.extend(values)
pos += run_length
return steps
Beispiele:
Code: Alles auswählen
>>> RLE([100] + 128*[101])
[130, 100, 101, 127, 101]
>>> RLE([100] + 128*[101] + [100])
[129, 100, 127, 101, 130, 101, 100]
>>> RLE([100] + 128*[101] + [100,100])
[130, 100, 101, 127, 101, 2, 100]