Run Length Encoding (RLE)

Implement RLE in Python.

Run-length encoding (RLE) is a simple form of data compression that encodes consecutive repeated values as counts. For black and white images, we represent black pixels as 0 and white pixels as 1.

In this “white-first” RLE variant:

For example, the sequence:

0 0 0 0 1 1 1 0 0 0

Would be encoded as:

[0, 4, 3, 3]

Meaning: 0 whites, then 4 blacks, then 3 whites, then 3 blacks

The example code will print the original, compressed, and uncompressed image, and show you the compression ratio.

You can find the starter code here:

https://github.com/scienceacademy/apcsp2025/blob/main/rle.py

Task:

  1. Complete the encode_rle() function to compress a 2D binary image

  2. Complete the decode_rle() function to decompress RLE data back to a 2D image

You can uncomment the “run_test_cases()” line at the end to test if your code is working.

Try your code with the following test images. Can you see why the compression ratios are so different?

test_image = [
[0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0]

]
test_image = [
[1, 1, 0, 0, 0, 0, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 1],
[0, 1, 0, 1, 1, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 1, 1, 0, 1, 0],
[0, 1, 1, 0, 0, 1, 1, 0],
[1, 0, 1, 1, 1, 1, 0, 1],
[1, 1, 0, 0, 0, 0, 1, 1]
]

test_image = [
[1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 1, 1, 0, 1, 1],
[1, 0, 1, 0, 0, 1, 0, 1],
[0, 1, 1, 1, 1, 1, 1, 0]
]

How to Submit

submit50 scienceacademy/problems/2025ap/rle