Run-length encoding: encoding text

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Run-length encoding: encoding text

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is AAAABBB run-length encoded?
A 4A3B
B A4B3
2. How many runs are in the string WWWBBWWWW?
A 3 runs
B 2 runs
3. Which is the correct run-length encoding of the string CCCCCC?
A C6
B 6C
4. How is a single X on its own written in run-length encoding?
A 1X
B X
5. Which is the correct encoding of AABCCC?
A 2A3C
B 2A1B3C

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1AAAABBB → 4A3B AOne run of 8 identical characters is written as the count 8 then the letter W
2WWWWWWWW → 8W BEach run of identical characters is replaced by a count followed by the character
3A single character that is not repeated still gets a count of 1 CThe single B is missing its count. It should be written 1B, so the answer is 2A1B3C.
4Count the runs first, then write each count followed by its character DAAABCC → 3A1B2C
5AAAABBB → A4B3 EThe character is written before the count. The count must come first, so it should be 4A3B.
6AABCCC → 2AB3C FXXYYYZZZZ → 2X3Y4Z

Definitions

4A3B

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
Encode AAAABBB: _A3B
2.
Encode WWWWWWBB: 6W_B
3.
Encode ABBB: _A3B
4.
Encode CCCDD: 3_2D
5.
Encode AABAAA: 2A1B_A

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Input: AAAABBB
B Runs: AAAA, BBB
C Counts: 4 and 3
D Output: A4B3
Question 2
A Input: WWWWWB
B Runs: WWWWW, B
C Output: 5WB
Question 3
A Input: CCDDDDE
B Runs: CC, DDDD, E
C Counts: 2, 4, 1
D Output: 2C3D1E
Question 4
A Input: ZZZZZZ
B Run length: 5
C Output: 6Z
Question 5
A text = AAABCC
B runs = AAA, B, CC
C encoded = 3A2B2C

Answer Key

Run-length encoding: encoding text
Section A: Quick Questions
Q1
A — 4A3B
Q2
A — 3 runs
Q3
B — 6C
Q4
A — 1X
Q5
B — 2A1B3C
Section B: Match the Pairs
Q1
AAAABBB → 4A3B → Each run of identical characters is replaced by a count followed by the character
Q2
WWWWWWWW → 8W → One run of 8 identical characters is written as the count 8 then the letter W
Q3
A single character that is not repeated still gets a count of 1 → AAABCC → 3A1B2C
Q4
Count the runs first, then write each count followed by its character → XXYYYZZZZ → 2X3Y4Z
Q5
AAAABBB → A4B3 → The character is written before the count. The count must come first, so it should be 4A3B.
Q6
AABCCC → 2AB3C → The single B is missing its count. It should be written 1B, so the answer is 2A1B3C.
Section C: Definitions
Q1
4A3B — Run-length encoding: each run of identical characters becomes a count then the character, so AAAABBB is written 4A3B. A single character is written 1X.
Section D: Fill in the Blanks
Q1
Encode AAAABBB: 4A3B
Q2
Encode WWWWWWBB: 6W2B
Q3
Encode ABBB: 1A3B
Q4
Encode CCCDD: 3C2D
Q5
Encode AABAAA: 2A1B3A
Section E: Spot the Error
Q1
D — Run-length encoding writes the count first, then the character. The output should be 4A3B, not A4B3.
Q2
C — A single character still needs a count of 1. The lone B must be written 1B, so the output is 5W1B.
Q3
D — The run of D is four letters long, as the Counts line says. It should be 4D, so the output is 2C4D1E.
Q4
B — ZZZZZZ has six Z characters, so the run length is 6, not 5. This matches the output 6Z.
Q5
C — There is only one B in the text, so it should be written 1B. The encoded string should be 3A1B2C.