Solution #5266c9ec-6ff5-445e-8459-1d713ef48118
failedScore
0% (0/5)
Runtime
1.20ms
Delta
-100.0% vs parent
-100.0% vs best
Regression from parent
Score
0% (0/5)
Runtime
1.20ms
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 len(data) == 0:
return 999.0
# Implement LZ77 compression as a different approach
class LZ77Compressor:
def __init__(self, window_size=20, buffer_size=15):
self.window_size = window_size
self.buffer_size = buffer_size
def compress(self, input_string):
i = 0
compressed_data = []
while i < len(input_string):
match_length = 0
match_offset = 0
for j in range(max(0, i - self.window_size), i):
length = 0
while (j + length < i and i + length < len(input_string) and
input_string[j + length] == input_string[i + length]):
length += 1
if length > match_length:
match_length = length
match_offset = i - j
if match_length > 0:
compressed_data.append((match_offset, match_length, input_string[i + match_length]))
else:
compressed_data.append((0, 0, input_string[i]))
i += match_length + 1
return compressed_data
def decompress(self, compressed_data):
decompressed_data = []
for offset, length, next_char in compressed_data:
if offset > 0:
start = len(decompressed_data) - offset
end = start + length
decompressed_data.extend(decompressed_data[start:end])
decompressed_data.append(next_char)
return ''.join(decompressed_data)
compressor = LZ77Compressor()
compressed_data = compressor.compress(data)
decompressed_data = compressor.decompress(compressed_data)
if decompressed_data != data:
return 999.0
# Calculate sizes
original_size = len(data) * 8 # in bits (assuming 8 bits per character)
# Each LZ77 tuple can be encoded with bits for offset, length, and the next character.
# Calculate how many bits approximately each tuple requires
max_bits = self.window_size.bit_length() + self.buffer_size.bit_length() + 8
compressed_size = len(compressed_data) * max_bits
return compressed_size / float(original_size)