Run-length encoding: decoding

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

Run-length encoding: decoding

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does the run-length code 3C2D decode to?
A CCDDD
B CCCDD
2. What does the run-length code 4A1B decode to?
A AAAAB
B ABBBB
3. In the pair 6M, which part tells you how many times to write the letter?
A The 6, because the count comes first
B The M, because the letter comes first
4. How long is the decoded text for 5X3Y?
A 4 characters, the length of the code
B 8 characters, because 5 + 3 = 8
5. What does 12Z decode to?
A 12 Zs in a row
B 2 Zs, because only the last digit counts

Match the Pairs

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

Left columnRight column
14A3B ADecodes to AAAABBB: write A four times, then B three times
212X1Y B10 was read as two separate digits. It is one two-digit count, so 10Z decodes to ZZZZZZZZZZ (ten Zs).
3Decoding reads the count first, then the character that gets repeated that many times CDecodes to XXXXXXXXXXXXY: 12 copies of X then one Y, which is 13 characters long
4To check a decode, add up all the counts: the total must equal the length of the decoded text DThe counts were swapped onto the wrong characters. 3C means three Cs and 2D means two Ds, so it should be CCCDD.
53C2D decoded as CCDDD E3C2D = CCCDD
610Z decoded as 1Z and then 0 (Z0) F2A5B -> 2 + 5 = 7 characters (AABBBBB)

Definitions

3C2D = CCCDD

Fill in the Blanks

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

1.
3C2D decodes to CCC__
2.
In 4A1B, the count for A is _
3.
3C2D: 3 + 2 = _ characters when decoded
4.
12X decodes to X repeated __ times
5.
In 5Q, the _____ is 5 and the character is Q

Spot the Error

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

Question 1
A Decode: 3C2D
B 3C → CCC
C 2D → D
D Result: CCCDD
Question 2
A encoded ← "X3"
B count ← 3
C char ← "X"
D decoded ← "XXX"
Question 3
A encoded ← 2A3B
B decoded ← AABBB
C length ← 2 * 3
D PRINT length
Question 4
A encoded ← 12Z
B count ← 1
C char ← "Z"
D decoded ← Z repeated 12 times
Question 5
A encoded ← 10A2B
B 10A → AAAAA
C 2B → BB
D length ← 10 + 2

Answer Key

Run-length encoding: decoding
Section A: Quick Questions
Q1
B — CCCDD
Q2
A — AAAAB
Q3
A — The 6, because the count comes first
Q4
B — 8 characters, because 5 + 3 = 8
Q5
A — 12 Zs in a row
Section B: Match the Pairs
Q1
4A3B → Decodes to AAAABBB: write A four times, then B three times
Q2
12X1Y → Decodes to XXXXXXXXXXXXY: 12 copies of X then one Y, which is 13 characters long
Q3
Decoding reads the count first, then the character that gets repeated that many times → 3C2D = CCCDD
Q4
To check a decode, add up all the counts: the total must equal the length of the decoded text → 2A5B -> 2 + 5 = 7 characters (AABBBBB)
Q5
3C2D decoded as CCDDD → The counts were swapped onto the wrong characters. 3C means three Cs and 2D means two Ds, so it should be CCCDD.
Q6
10Z decoded as 1Z and then 0 (Z0) → 10 was read as two separate digits. It is one two-digit count, so 10Z decodes to ZZZZZZZZZZ (ten Zs).
Section C: Definitions
Q1
3C2D = CCCDD — Decoding run-length data: read each count then its character, and write that character that many times. 3C means CCC and 2D means DD, so 3C2D expands back to CCCDD.
Section D: Fill in the Blanks
Q1
3C2D decodes to CCCDD
Q2
In 4A1B, the count for A is 4
Q3
3C2D: 3 + 2 = 5 characters when decoded
Q4
12X decodes to X repeated 12 times
Q5
In 5Q, the count is 5 and the character is Q
Section E: Spot the Error
Q1
C — The count 2 in 2D means the character D is written two times, so it should be 2D → DD. Writing only one D drops a copy and the decoded text would be too short.
Q2
A — In run-length data the count comes first and the character second, so the code should be "3X". "X3" has the pair the wrong way round and would not decode to XXX.
Q3
C — The decoded length is the sum of the counts, not their product. It should be 2 + 3 = 5, which matches AABBB. Multiplying gives 6, which is wrong.
Q4
B — 12Z has a two-digit count. Both digits belong together, so the count is 12, not just the first digit 1. It should be count ← 12.
Q5
B — 10A means ten copies of A, but this line only writes five. It should be 10A → AAAAAAAAAA, with ten As.