Skip to content

Run-length encoding

words

Level 1 of 3

Also called RLE.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

The method runs on bbbaacc. How many characters has it written just after it reads the first c?

1234567bbbaacc
the answer, and why

the answer is C

Reading the first a ended bbb, so 3b. Reading the first c ended aa, so 2a. 3b2a is 4 characters; cc is written only at the end.

if you chose another

  • That is 3b alone. Reading the first c also writes aa's run, 2a.
  • That is the whole code, 3b2a2c. The last run is written only at the end.
  • That counts the runs met so far, bbb, aa and c. Only runs that have ended are written.

question 2

How many comparisons does the method make on the text bbbaacc?

1234567bbbaacc
the answer, and why

the answer is C

Each letter after the first is held against the run's letter once. 7 letters, so 6 comparisons.

if you chose another

  • The first letter is compared with nothing. It starts the first run.
  • That counts the runs. Every letter after the first is compared, in a run or not.
  • That counts only the comparisons that find a different letter. Those that find the same count too.

question 3

Which text does the code 2p3q1r stand for?

the answer, and why

the answer is C

2 p's, then 3 q's, then 1 r. So ppqqqr.

this tests how it works, on the learn page

if you chose another

  • That reads the first count as 1. 2p is two p's.
  • That writes 2 r's. The count before r is 1.
  • That drops the counts, so every run is read as 1 letter.

question 4

A text grows from 1,000 letters to 2,000. What happens to the comparisons?

the answer, and why

the answer is A

By the learn page's question 1, CC is n−1n - 1. 999 for 1,000 letters, 1,999 for 2,000.

this tests where the formula comes from, on the learn page

if you chose another

  • The code may stay short, but every letter after the first is still compared.
  • Each of the 1,000 new letters is compared, not just one.
  • That would be every letter compared with every other. Each is compared once, with the run's letter.

pen and paper

question 5about 5 minutes

Run the method by hand on the text xxyyyyz.

  1. Each comparison, and whether the letters match.
  2. The code.
  3. The text read back from the code.
a hint

Write a run only when the next letter differs, or at the end.

a worked answer

The second x matches x. The first y differs, so write 2x. The next three y's match. z differs, so write 4y. At the end, write 1z. That is 6 comparisons, and the code 2x4y1z. Read back, 2x4y1z gives xxyyyyz.

letterheld againstsamecomparisons so far
x--0
xxyes1
yxno2
yyyes3
yyyes4
yyyes5
zyno6
xxyyyyz: the first x starts a run. Each of the other 6 letters is compared once.

this tests how it works, on the learn page

what a full answer includes
  • 6 comparisons: match, differ, match, match, match, differ.
  • 2x4y1z.
  • xxyyyyz.

question 6about 4 minutes

One text of 1,000 letters goes x, y, z, x, y, z and so on. Another is 1,000 a's.

  1. How many comparisons does each take?
  2. How many characters does each code have?
a hint

Use questions 1 and 2 of the learn page.

a worked answer

By the learn page's question 1, each makes 999 comparisons, one for each letter after the first. By the learn page's question 2, the first has no letter twice in a row, so its code has 2,000 characters, twice its length. The second is one run, 1000a, 5 characters.

Question 2: how many characters, WW, does the method write for a text of nn letters with no letter twice in a row? And why does no text of nn letters write more?
  1. the move: every run is one letter

    the line:
    no letter twice in a row:n runs of 1 letter\begin{gathered} \text{no letter twice in a row:} \\ n \text{ runs of 1 letter} \end{gathered}

    why it is allowed: The first letter starts a run. Each other letter differs from the one before, so it starts a new run, ended by the next letter or the end. xyzx has 4 runs.

  2. the move: count them

    the line:
    W=2nW = 2n

    why it is allowed: By line 1, nn runs, and each writes 1, then its letter, 2 characters. For xyzx, 1x1y1z1x, 8.

  3. the move: a longer run

    the line:
    a run of L letters writesthe digits of L, then 1 letter\begin{gathered} \text{a run of } L \text{ letters writes} \\ \text{the digits of } L\text{, then 1 letter} \end{gathered}
    runLwritescharacters
    b11b2
    aaa33a2
    zzzzzzzzzz1010z3
    A run of 1 writes twice its letters. Longer runs write fewer.

    why it is allowed: The method writes each run as its length, then its letter. aaa writes 3a, 2 characters; ten z's write 10z, 3, as 10 has two digits.

  4. the move: put the digits' limit into line 3

    the line:
    from L of 2 up, a run writesat most L characters\begin{gathered} \text{from } L \text{ of 2 up, a run writes} \\ \text{at most } L \text{ characters} \end{gathered}
    Ldigitscharacterstwice L
    2124
    91218
    102320
    10034200
    From 2 letters up, a run writes at most its letters, and far under twice them.

    why it is allowed: 2 to 9 have 1 digit, and 1 is at most LL less 1. 10 to 99 have 2 digits and are at least 10; 100 to 999 have 3 and are at least 100. Each extra digit makes the smallest LL 10 times bigger but adds only 1 digit. So from 2 up, the number LL has at most LL less 1 digits; line 3 adds 1 letter, so at most LL.

  5. the move: each run at most twice its letters

    the line:
    a run of L letters writesat most 2L\begin{gathered} \text{a run of } L \text{ letters writes} \\ \text{at most } 2L \end{gathered}
    runLwritestwice L
    aa224
    b122
    ccc326
    total6612
    aabccc, each run writes at most twice its letters, so the code, 6 characters, is at most twice the text, 12.

    why it is allowed: A run of 1 writes 2 by line 3. From 2 up, at most LL by line 4, under 2L2L.

  6. the move: add up the runs

    the line:
    any text of n letterswrites at most 2n\begin{gathered} \text{any text of } n \text{ letters} \\ \text{writes at most } 2n \end{gathered}

    why it is allowed: By line 5, and the runs' letters add up to nn, so the code has at most 2n2n characters. Exactly 2n2n only when every run is 1 letter, as by line 5 a longer run writes at most LL, under 2L2L.

try it with a small nn: at n=4n = 4, line 2 gives W=8W = 8, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 999 each.
  • 2,000 and 5.