← IGCSE Computer Science Paper 1

Decoding run-length encoded image data

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.

Understanding Decoding run-length encoded image data in IGCSE Computer Science Paper 1

What it does: The count 2 repeats G twice and the count 3 repeats Y three times, which rebuilds the row in order. The counts add to 9, so the row is correct for an image 9 pixels wide. Any other total means the data or decoding is wrong. Work left to right: 2W gives WW, 1K gives K, 3W gives WWW. Together they make WWKWWW. In run-length encoding the number is the count. It says how many pixels in a row have that colour. 4 + 3 + 2 = 9 pixels. A valid row must add up to the image width, so this row is one pixel too long.

Common mistakes: 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. 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 86–109) and word search (Modules 86–120).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Decode this run-length encoded row: 3R2B
RRBBBRRRBB
Matches
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.
Applying
Meanings
Term
RLE decode
Meaning
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.
Practice
Encoded: 3R2B1G
Testing
Gaps
3R2B decodes to
Check Answer
Spots
3for count, colour in data:
4 row = colour * count

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Cheetah' badge — Savannah Sprinter, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Decode this run-length encoded row: 3R2B
A. RRRBB ✓
B. RRBBB
The count comes before the colour code. 3R means R three times and 2B means B twice, so the row is RRRBB.
2. Decode this run-length encoded row (W = white, K = black): 2W1K3W
A. WWKWWW ✓
B. WWWKWW
Work left to right: 2W gives WW, 1K gives K, 3W gives WWW. Together they make WWKWWW.
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
In run-length encoding the number is the count. It says how many pixels in a row have that colour.
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 ✓
4 + 3 + 2 = 9 pixels. A valid row must add up to the image width, so this row is one pixel too long.
5. The image is 6 pixels wide. Which is the correct decoding of the row 1R4B1R?
A. RBBBR
B. RBBBBR ✓
1R gives R, 4B gives BBBB and 1R gives R. That makes 6 pixels, which matches the width. RBBBR has only 5 pixels.

Match the Pairs

3R2B → Decodes to RRRBB: three red pixels followed by two blue pixels
1W4K1W → Decodes to WKKKKW: one white, four black, then one white pixel, a row 6 pixels wide
To rebuild a row, repeat each colour code as many times as its count says, working left to right → 2G3Y -> GGYYY
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
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.
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.

Key Term

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.

Fill in the Blanks

1. 3R2B decodes to RRRBB
Hint: Type the decoded row of pixels, repeating each colour code its count number of times
2. 4W1K2W decodes to WWWWKWW
Hint: Type the count that gives the right number of white pixels at the start of the row
3. 3R2B1G: 3 + 2 + 1 = 6 pixels wide
Hint: Type the total of all the counts, which is the width of the decoded row
4. FOR i ← 1 TO count row ← row + colour NEXT i
Hint: Type the variable that says how many times the colour code is repeated
5. IF LENGTH(row) = width THEN OUTPUT "Row is correct" ENDIF
Hint: Type the operator that checks the rebuilt row is exactly equal to the image width

Spot the Error

Question 1
A data = [(3, "R"), (2, "B")]
B row = ""
C for count, colour in data:
D row = colour * count
E print(row)
Line 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.
Question 2
A runs = [(2, "W"), (3, "K")]
B row = ""
C for n, c in runs[0]:
D row += c * n
E print(row)
Line 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.
Question 3
A row = "RRBBB"
B width = len(row)
C ok = len(row) == width
D print(ok)
Line 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.
Question 4
A row = "RRRBB"
B width = 5
C if row == width:
D print("Row OK")
Line 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.
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)
Line 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.

Student with a class code? Join your class →