Solution #cfe29333-54b2-4967-b681-c147077458e0
failedScore
0% (0/5)
Runtime
1.59ms
Delta
-100.0% vs parent
-100.0% vs best
Regression from parent
Score
0% (0/5)
Runtime
1.59ms
Delta
-100.0% vs parent
-100.0% vs best
Regression from parent
def solve(input):
data = input.get("data", "")
if not isinstance(data, str) or not data:
return 999.0
# Implement a simple dictionary-based compression (LZ77 variant)
def lz77_compress(text):
# Initialize window and buffer sizes
window_size = 256
buffer_size = 15
compressed = []
i = 0
while i < len(text):
match_length = 0
match_distance = 0
# Look for the longest match in the sliding window
for j in range(max(0, i - window_size), i):
k = 0
while k < buffer_size and i + k < len(text) and text[j + k] == text[i + k]:
k += 1
if k > match_length:
match_length = k
match_distance = i - j
if match_length >= 3: # If match is found and is worthwhile
compressed.append((match_distance, match_length, text[i + match_length] if i + match_length < len(text) else ''))
i += match_length + 1
else:
compressed.append((0, 0, text[i]))
i += 1
return compressed
def lz77_decompress(compressed):
decompressed = []
for distance, length, next_char in compressed:
if length > 0:
start_index = len(decompressed) - distance
decompressed.extend(decompressed[start_index:start_index + length])
if next_char:
decompressed.append(next_char)
return ''.join(decompressed)
# Compress and Decompress
compressed_data = lz77_compress(data)
decompressed_data = lz77_decompress(compressed_data)
if decompressed_data != data:
return 999.0
original_size = len(data) * 8
compressed_size = sum(8 + 8 + 8 for _, _, next_char in compressed_data if next_char) + sum(8 + 8 for _, length, _ in compressed_data if length > 0)
if original_size == 0:
return 999.0
compression_ratio = compressed_size / original_size
return 1.0 - compression_ratio