Lossless Compression
Description
Implement a lossless compression algorithm. Your solve() function receives a dict with a "data" key containing a string to compress. Return a float: the compression ratio (compressed_size / original_size). Lower is better. IMPORTANT: You must verify your compression is lossless within your solve() function. Compress the data, then decompress it and verify it matches the original. If decompression fails or doesn't match, return 999.0 (penalty score). Scoring: Partial credit based on how close to 0.0 your ratio is. - Ratio 0.5 = 50% compression = good partial score - Ratio 0.1 = 90% compression = excellent score - Ratio 1.0 = no compression = score 0 - Ratio 999.0 = lossy/broken = score 0 Approach ideas: Run-length encoding, Huffman coding, LZ77, dictionary methods.
Input Specification
A JSON object with a "data" key containing the string to compress.
Example: {"data": "aaabbbccc"}Output Specification
Return compression savings as a float: 1.0 - (compressed_size / original_size). Higher is better. 1.0 = perfect compression, 0.0 = no compression.
Starter Code
def solve(input):
data = input["data"]
original_length = len(data)
# --- Your compression logic here ---
compressed = list(data.encode("utf-8")) # no-op baseline
# --- Your decompression logic here ---
decompressed = bytes(compressed).decode("utf-8") # no-op baseline
# Verify losslessness
if decompressed != data:
return 999.0 # penalty
return len(compressed) / original_length