Decoding run-length encoded image data

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

Decoding run-length encoded image data

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Decode this run-length encoded row: 3R2B
A RRRBB
B RRBBB
2. Decode this run-length encoded row (W = white, K = black): 2W1K3W
A WWKWWW
B WWWKWW
3. In the encoded data 5G, what does the number 5 tell you?
A Repeat the colour code G five times
B Add 5 to the colour code G
4. An image is 8 pixels wide. A row is encoded as 4B3W2B. Is this a valid row for the image?
A Yes - the counts add up to 8
B No - the counts add up to 9, not 8
5. The image is 6 pixels wide. Which is the correct decoding of the row 1R4B1R?
A RBBBR
B RBBBBR

Match the Pairs

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

Left columnRight column
13R2B ADecodes to RRRBB: three red pixels followed by two blue pixels
21W4K1W BThe number and the colour code have been copied as they are instead of being repeated. The count must be used to repeat the colour code, so 3R2B should give RRRBB.
3To rebuild a row, repeat each colour code as many times as its count says, working left to right C4B2W3B -> 4+2+3 = 9
4To check a decoded row has the right width, add up all the counts and compare the total with the image width D2G3Y -> GGYYY
53R2B decoded as R3B2 EThe decoded row has only 7 pixels (2+3+2 = 7), but the image width is 8. The check should show the row is one pixel short, so it is wrong.
6Image width 8, row 2R3B2R decoded as RRBBBRR and accepted as correct FDecodes to WKKKKW: one white, four black, then one white pixel, a row 6 pixels wide

Definitions

RLE decode

Fill in the Blanks

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

1.
3R2B decodes to _____
2.
_W1K2W decodes to WWWWKWW
3.
3R2B1G: 3 + 2 + 1 = _ pixels wide
4.
FOR i ← 1 TO _____ row ← row + colour NEXT i
5.
IF LENGTH(row) _ width THEN OUTPUT "Row is correct" ENDIF

Spot the Error

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

Question 1
A data = [(3, "R"), (2, "B")]
B row = ""
C for count, colour in data:
D row = colour * count
E print(row)
Question 2
A runs = [(2, "W"), (3, "K")]
B row = ""
C for n, c in runs[0]:
D row += c * n
E print(row)
Question 3
A row = "RRBBB"
B width = len(row)
C ok = len(row) == width
D print(ok)
Question 4
A row = "RRRBB"
B width = 5
C if row == width:
D print("Row OK")
Question 5
A grid = []
B for runs in image:
C row = ""
D for n, c in image:
E row += c * n
F grid.append(row)

Answer Key

Decoding run-length encoded image data
Section A: Quick Questions
Q1
A — RRRBB
Q2
A — WWKWWW
Q3
A — Repeat the colour code G five times
Q4
B — No - the counts add up to 9, not 8
Q5
B — RBBBBR
Section B: Match the Pairs
Q1
3R2B → Decodes to RRRBB: three red pixels followed by two blue pixels
Q2
1W4K1W → Decodes to WKKKKW: one white, four black, then one white pixel, a row 6 pixels wide
Q3
To rebuild a row, repeat each colour code as many times as its count says, working left to right → 2G3Y -> GGYYY
Q4
To check a decoded row has the right width, add up all the counts and compare the total with the image width → 4B2W3B -> 4+2+3 = 9
Q5
3R2B decoded as R3B2 → The number and the colour code have been copied as they are instead of being repeated. The count must be used to repeat the colour code, so 3R2B should give RRRBB.
Q6
Image width 8, row 2R3B2R decoded as RRBBBRR and accepted as correct → The decoded row has only 7 pixels (2+3+2 = 7), but the image width is 8. The check should show the row is one pixel short, so it is wrong.
Section C: Definitions
Q1
RLE decode — Repeat each colour code count times along the row to rebuild the pixels, e.g. 3R2B gives RRRBB. Then check the row adds up to the image width.
Section D: Fill in the Blanks
Q1
3R2B decodes to RRRBB
Q2
4W1K2W decodes to WWWWKWW
Q3
3R2B1G: 3 + 2 + 1 = 6 pixels wide
Q4
FOR i ← 1 TO count row ← row + colour NEXT i
Q5
IF LENGTH(row) = width THEN OUTPUT "Row is correct" ENDIF
Section E: Spot the Error
Q1
D — Using = replaces the row on every pass, so only the last run survives. Use += to add each repeated colour code to the end of the row, giving RRRBB.
Q2
C — runs[0] is only the first (count, colour) pair, so the loop does not go through every run. Loop over runs so all runs are decoded.
Q3
B — The image width is a known value (5 pixels), not something worked out from the row. Setting width = len(row) makes the check always pass, so it can never spot a row of the wrong length. It should be width = 5.
Q4
C — This compares the row text with a number, so it is never true. Use len(row) to count the decoded pixels and compare that with the width.
Q5
D — The inner loop must go through the runs of the current row, not the whole image. It should be for n, c in runs: so each row is decoded from its own runs.