Editor

Decode Run-Length Text

Intermediate · Write the code · from Data Compression: Lossless vs Lossy, with Run-Length Encoding

From the lesson: What is the difference between lossless and lossy compression?

Lossless compression algorithms can usually reduce the number of bits stored or sent while guaranteeing that the original can be rebuilt exactly, bit for bit. RLE and zlib are both lossless, and so are ZIP files and PNG images. Decoding RLE is a matter of repeating each symbol by its count:

Read this part of the lesson

Write rle_decode(code), which takes run-length code written as a string, such as '4A2B1C', and returns the original text, 'AAAABBC'. A count can have more than one digit ('12W' means twelve W's), and the original text never contains digits.

def rle_decode(code):
# Collect digits until you reach a symbol, then repeat the symbol
pass

What Check runs

Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.

RunsExpected output
print(rle_decode("4A2B1C"))AAAABBC
print(rle_decode("3x"))xxx
print(rle_decode("1X1Y1Z"))XYZ

More Data Compression exercises

All Python exercises · 50 practice problems