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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
RLE decodeFill in the Blanks
Spot the Error
Student with a class code? Join your class →