Runic Compression: Run-Length Encoding
๐ฆ Runic Compression: Run-Length Encoding
Difficulty: Beginner Tags: strings, compression, iteration, encoding Series: CS 102: Problem Solving & Logic
Problem
Ancient scholars compress their runic texts using run-length encoding. Transform consecutive identical characters into a compact format.
Implement run-length encoding: compress sequences of consecutive identical characters into the format <character><count>. Even single characters should include their count of 1.
Real-World Application
Run-length encoding appears in: - Image compression - BMP, TIFF file formats for simple graphics - Data transmission - reducing bandwidth for repetitive data - Fax machines - compressing black and white documents - Video compression - early codecs, animation compression - Genetic sequencing - compressing DNA sequences - Network protocols - compressing repetitive packet data - Database storage - column-oriented databases - Text compression - preprocessing step for other algorithms
Input
data = str # String to compress
Output
str # Run-length encoded string
Constraints
- String length โค 100,000
- ASCII characters
- Single characters must include count 1
Examples
Example 1: Basic Compression
Input: 'aaabbc'
Output: 'a3b2c1'
Explanation: - 'aaa' (3 consecutive a's) โ 'a3' - 'bb' (2 consecutive b's) โ 'b2' - 'c' (1 c) โ 'c1'
Example 2: Empty String
Input: ''
Output: ''
Explanation: - Empty input returns empty output
Example 3: Single Character
Input: 'a'
Output: 'a1'
Explanation: - Single character still includes count
Example 4: No Consecutive Characters
Input: 'abcd'
Output: 'a1b1c1d1'
Explanation: - Each character appears once - All get count 1
Example 5: Long Run
Input: 'aaaaaaaaaa' (10 a's)
Output: 'a10'
Explanation: - 10 consecutive a's compressed to 'a10'
Example 6: Multiple Runs
Input: 'aabbbaabbbbbb'
Output: 'a2b3a2b6'
Explanation: - First run: 'aa' โ 'a2' - Second run: 'bbb' โ 'b3' - Third run: 'aa' โ 'a2' - Fourth run: 'bbbbbb' โ 'b6'
Example 7: With Spaces and Punctuation
Input: 'aaa bbb'
Output: 'a3 3b3'
Explanation: - 'aaa' โ 'a3' - ' ' (3 spaces) โ ' 3' - 'bbb' โ 'b3'
What You'll Learn
- Detecting run boundaries in sequences
- Building output strings efficiently
- Two-pointer technique for consecutive elements
- Counting consecutive occurrences
- String concatenation strategies
Why This Matters
Run-length encoding teaches: - Fundamental compression concepts - Pattern recognition in sequences - Efficient iteration techniques - Real-world data processing
This is a common interview question testing string manipulation skills.
Starter Code
def challenge_function(data):
"""
Perform run-length encoding on string.
Args:
data: str to compress
Returns:
str: run-length encoded string (format: char + count)
"""
# Your implementation here
pass