Run-length encoding (compression) · Codificación por longitud de corrida (compresión)
Squeezing repeated data
- Compression makes data smaller. Run-length encoding (RLE) is one simple way.
- It works well when the same value repeats many times in a row.
- It is lossless: from the squeezed form you can rebuild the exact original.
Comprimiendo datos repetidos
- La compresión hace que los datos sean más pequeños. El codificación por recuento de secuencias (RLE, por sus siglas en inglés) es una forma sencilla de lograrlo.
- Funciona bien cuando el mismo valor se repite muchas veces consecutivamente.
- Es sin pérdida: a partir de la forma comprimida puedes reconstruir exactamente el original.
What a "run" is
- A run is a stretch of the same character repeated: in
"aaabbc","aaa"is a run of 3. - RLE replaces each run with the character followed by how many times it repeats.
- So
"aaabbc"becomes"a3b2c1"— much shorter when runs are long.
Qué es una "secuencia"
- Una secuencia es un tramo del mismo carácter repetido: en
"aaabbc","aaa"es una secuencia de 3. - RLE reemplaza cada secuencia con el carácter seguido de cuántas veces se repite.
- Así,
"aaabbc"se convierte en"a3b2c1"— mucho más corto cuando las secuencias son largas.
Counting a run
- To measure a run, look at a character, then count how many of the same character follow it.
- Stop when the next character is different, or you reach the end (
'\0'). - That count is the run's length.
Contando una secuencia
- Para medir una secuencia, observa un carácter y cuenta cuántos caracteres iguales lo siguen.
- Detente cuando el siguiente carácter sea diferente o reaches el final (
'\0'). - Ese conteo es la longitud de la secuencia.
Building the encoded string
- Walk the input. For each run, write the character, then its count, into the output.
- Move your input position past the whole run before starting the next one.
- End the output string with
'\0'so it is a proper C string.
Construyendo la cadena codificada
- Recorre la entrada. Por cada secuencia, escribe el carácter, luego su conteo, en la salida.
- Mueve tu posición de entrada más allá de toda la secuencia antes de comenzar la siguiente.
- Termina la cadena de salida con
'\0'para que sea una cadena C válida.
#include <stdio.h>
int main(void) {
const char *s = "aaab";
int i = 0;
char c = s[i];
int count = 0;
while (s[i] == c) { // count the first run
count++;
i++;
}
printf("%c%d\n", c, count); // a3
return 0;
}
Common mistakes
- Run-length encoding stores a value then its count; it only helps when there are long runs.
- It is lossless — the original is restored exactly.
Errores comunes
- La codificación por recuento de secuencias almacena un valor seguido de su conteo; solo es útil cuando hay secuencias largas.
- Es sin pérdida: el original se restaura exactamente.
Now you try
- Find each run, then write the character and its count to the output.
- The caller gives you an output buffer big enough to hold the result. Do not write a
main.
Ahora tú intentas
- Encuentra cada secuencia, luego escribe el carácter y su conteo en la salida.
- El llamador te proporciona un búfer de salida lo suficientemente grande para contener el resultado. No escribas una función
main.
Run-length encoding · Codificación por longitud de corrida
Replace a run of repeats with count + symbol — lossless. · Reemplazar una secuencia de repeticiones con cantidad + símbolo — sin pérdida.
Complete int run_length_at(const char *s, int i) so it returns how many times the character s[i] repeats starting at index i. Do not · no write a main. · Complete int run_length_at(const char *s, int i) para que devuelva cuántas veces se repite el carácter s[i] comenzando en el índice i. No escriba un main.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Complete void rle_encode(const char *in, char *out) so it writes the run-length encoding of in into · dentro de out: each run becomes the character then its count. "aaabbc" becomes "a3b2c1". End out with '\0'. Do not · no write a main. · Complete void rle_encode(const char *in, char *out) para que escriba la codificación por longitud de corrida de in dentro de out: cada corrida se convierte en el carácter seguido de su cantidad. "aaabbc" se convierte en "a3b2c1". Termine out con '\0'. No escriba un main.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.