Measure the Saving
Beginner · Write the code · from Data Compression: Lossless vs Lossy, with Run-Length Encoding
From the lesson: How much space does compression save?
To measure a compression algorithm, compare the size before and after. The program below counts characters; each one takes the same number of bits, so the percentage saved is the same either way.
Read this part of the lessonWrite percent_saved(original, compressed). Both arguments are strings, and size is measured in characters. Return the percentage of the original size that compression saved, rounded to 1 decimal place. If the compressed version is bigger, the result is negative.
percent_saved('AAAAAAAAAA', '10A') -> 70.0def percent_saved(original, compressed):
# (size before - size after) as a percentage of size before
pass
What Check runs
Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| print(percent_saved("AAAAAAAAAA", "10A")) | 70.0 |
| print(percent_saved("AAAABBC", "4A2B1C")) | 14.3 |
More Data Compression exercises
- Fix the Run-Length EncoderIntermediate
- Decode Run-Length TextIntermediate