Implementing run-length encoding and decoding in Python
This is a medium-difficulty coding problem that tests your ability to implement a practical lossless compression algorithm. You will build two complementary functions: one to compress data by replacing runs of identical characters with the character and its count, and another to decompress the result back to the original form.
The core challenge is handling the encoding rules precisely: single characters appear bare, while runs of length two or more include a count suffix. You must also decode a compressed string by parsing characters and their associated counts, then expanding them back. Strong solutions iterate cleanly through the input, track consecutive runs, and handle the parsing logic without off-by-one errors. Edge cases — such as single-character inputs, alternating characters with no runs, and multi-digit counts — require careful attention.
- Grouping and counting consecutive identical elements
- String parsing and number extraction
- Preserving sequence order through compression and decompression
- Handling boundary conditions between runs