Loading...
Compression changes how information is represented to reduce typical storage or transfer demands. Specify whether the exact original must be recoverable.
What you'll be able to do
Lossless compression permits exact reconstruction of the original data. It suits program files and text when every symbol matters. Lossy compression deliberately removes or approximates selected information, often exploiting perceptual properties of images or audio. Its decoded output need not equal the original samples.
File-size reduction does not by itself establish acceptable quality. A medical, scientific or archival task may value exact data differently from a small preview image. Repeated lossy re-encoding can introduce further changes, while decompressing a lossless file does not inherently remove information.
Run-length encoding stores a repeated consecutive value and its count. AABBBAA becomes (2,A),(3,B),(2,A); separated occurrences must not be merged into one run because their positions matter. It works well for long uniform areas but can expand rapidly alternating input.
Dictionary-based methods replace repeated strings or patterns with references to shared entries. The decoder needs the same dictionary or the rules to reconstruct it. Initial dictionary storage, reference widths and control information contribute overhead, so count the whole representation when calculating savings.
If original and compressed sizes are supplied, a compressed/original ratio below one indicates reduction under that convention. Other questions use original/compressed or percentage saving, so write the requested definition explicitly. No lossless method can make every possible input shorter.
Already compressed or high-variation data may offer little exploitable repetition. The best choice depends on time cost, memory, compatibility and permitted information loss as well as nominal size. Explain the mechanism suited to the input rather than claiming compression automatically improves all data.
| Input | Consecutive runs | Run count |
|---|---|---|
| AABBBAA | 2A, 3B, 2A | 3 |
| ABABABA | 1A, 1B, 1A, 1B, 1A, 1B, 1A | 7 |
Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.