Solution #f1c25843-047d-4c09-82ca-9b30eace04bb
failedScore
0% (0/5)
Runtime
1.13ms
Delta
-100.0% vs parent
-100.0% vs best
Regression from parent
Score
0% (0/5)
Runtime
1.13ms
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
# Implementing LZ77 Compression Algorithm
def lz77_compress(uncompressed):
compressed = []
i = 0
while i < len(uncompressed):
match = ""
best_match_distance = 0
best_match_length = 0
# Look back at most 255 characters (arbitrary small sliding window)
start = max(0, i - 255)
for j in range(start, i):
length = 0
while (i + length < len(uncompressed) and
uncompressed[j + length] == uncompressed[i + length]):
length += 1
if j + length >= i:
break
if length > best_match_length:
best_match_distance = i - j
best_match_length = length
if best_match_length > 0:
compressed.append((best_match_distance, best_match_length, uncompressed[i + best_match_length]))
i += best_match_length + 1
else:
compressed.append((0, 0, uncompressed[i]))
i += 1
return compressed
def lz77_decompress(compressed):
decompressed = []
for distance, length, next_char in compressed:
start = len(decompressed) - distance
decompressed.extend(decompressed[start:start + length])
decompressed.append(next_char)
return ''.join(decompressed)
compressed_data = lz77_compress(data)
decompressed_data = lz77_decompress(compressed_data)
if decompressed_data != data:
return 999.0
# Calculating the compression ratio
compressed_size = sum(2 + 1 for _ in compressed_data) # each (distance, length, char) can be assumed to take 3 bytes
original_size = len(data)
return 1.0 - (compressed_size / float(original_size))