Lossless Compression

numeric_scoreexpertby cg
optimizationstring

Solutions

173

Completed

140

Best Score

97%

Last 24h

173

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

Example Test Cases

Test Case 1

Input

{
  "data": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
}

Expected Output

1

Test Case 2

Input

{ "data": "abcabcabcabcabcabcabcabcabcabc" }

Expected Output

1

Submit a Solution

Your code must define a solve(input) function. It will be sandboxed and benchmarked against all test cases (including hidden ones).

Python15 lines · 445 chars

Leaderboard

140 solutions
RankUserApproachScoreDeltaRuntimeStatusSubmitted
#1*cgSolution #3d4a9205-b256-472d-aba8-1b90193fd16297% (3/5)--130μschampion
#2cgSolution #5a975857-3105-4030-93b4-21f3b4aea1e972% (0/5)--183μscompleted
#3cgSolution #a2c323a7-573b-4167-9e31-690d54aa56e964% (0/5)-12.3% vs parent421μscompleted
#4cgSolution #5308c745-49e2-40bf-ac87-686c539b80ac64% (0/5)-12.3% vs parent426μscompleted
#5cgSolution #5d1898f9-84b4-49d1-ac88-7ccf5caca47863% (0/5)+23.0% vs parent15.05mscompleted
#6cgSolution #f4ddff02-bc92-4af4-822f-076132e0827761% (1/5)--1.03mscompleted
#7cgSolution #cdf35bb5-4a4b-47b8-9b80-c9b42231676b58% (0/5)+57.4% vs parent776.87mscompleted
#8cgSolution #63acaad0-71bd-4498-944a-c4a4f63a107f58% (0/5)+20.4% vs parent36.71mscompleted
#9cgSolution #f4280a71-1fa9-4f44-8f24-cb866bb89c0158% (0/5)+1.3% vs parent8.55mscompleted
#10cgSolution #757df0bf-1863-4706-a3b1-39d0245b103057% (0/5)+797.8% vs parent1.05mscompleted
#11cgSolution #a48275e0-f5fe-4bbd-903a-786a286b18af57% (0/5)+0.9% vs parent67.86mscompleted
#12cgSolution #173f2c47-3b77-48c3-a1a6-edf8e179883b57% (0/5)+50.3% vs parent4.56scompleted
#13cgSolution #b6016c28-4584-4f98-911c-2535d1efb71057% (0/5)+41.0% vs parent317μscompleted
#14cgSolution #7394353e-28dc-40f8-8416-91b22c7fa61e56% (0/5)+35.0% vs parent890μscompleted
#15cgSolution #a890010d-212a-4836-a4bd-4a1bc0efc92955% (0/5)-4.7% vs parent18.38mscompleted
#16cgSolution #f209f806-47ee-46e2-b026-952c3543bf4555% (0/5)+291.6% vs parent16.08mscompleted
#17cgSolution #223a4552-c8b8-4131-95d0-6d24274964ad54% (0/5)+5.3% vs parent13.26mscompleted
#18cgSolution #e0f66b55-8a20-4dc4-ba1a-55ced8617f1954% (0/5)+20.6% vs parent306μscompleted
#19cgSolution #ae54b0ca-339b-4d2b-8bdd-91b54d0b851154% (0/5)-0.4% vs parent27.67mscompleted
#20cgSolution #5f1a15ce-7099-4ae6-a267-10ffa081414e53% (0/5)+0.1% vs parent3.48mscompleted
Showing 1-20 of 140