Solution #e74e9384-884b-4b7d-8312-165e076af553
completedScore
20% (0/5)
Runtime
493μs
Delta
+4.8% vs parent
-79.2% vs best
Improved from parent
Score
20% (0/5)
Runtime
493μs
Delta
+4.8% vs parent
-79.2% vs best
Improved from parent
def solve(input):
data = input.get("data", "")
if not isinstance(data, str) or not data:
return 999.0
# Approach: Lempel-Ziv-Welch (LZW) Compression
def compress_lzw(uncompressed):
dictionary = {chr(i): i for i in range(256)}
dict_size = 256
w = ""
result = []
for c in uncompressed:
wc = w + c
if wc in dictionary:
w = wc
else:
result.append(dictionary[w])
if dict_size < 4096: # Limit dictionary size for practical use
dictionary[wc] = dict_size
dict_size += 1
w = c
if w:
result.append(dictionary[w])
return result
def decompress_lzw(compressed):
dictionary = {i: chr(i) for i in range(256)}
dict_size = 256
w = chr(compressed.pop(0))
result = [w]
for k in compressed:
if k in dictionary:
entry = dictionary[k]
elif k == dict_size:
entry = w + w[0]
else:
return None # Decompression failed
result.append(entry)
if dict_size < 4096: # Limit dictionary size for practical use
dictionary[dict_size] = w + entry[0]
dict_size += 1
w = entry
return ''.join(result)
compressed_data = compress_lzw(data)
decompressed_data = decompress_lzw(compressed_data)
if decompressed_data != data:
return 999.0
original_size = len(data)
compressed_size = len(compressed_data) * 2 # Assuming 16-bit integers for each entry
if original_size == 0:
return 999.0
compression_ratio = compressed_size / original_size
return 1.0 - compression_ratioScore Difference
-76.5%
Runtime Advantage
363μs slower
Code Size
61 vs 34 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | data = input.get("data", "") | 2 | data = input.get("data", "") |
| 3 | if not isinstance(data, str) or not data: | 3 | if not isinstance(data, str) or not data: |
| 4 | return 999.0 | 4 | return 999.0 |
| 5 | 5 | ||
| 6 | # Approach: Lempel-Ziv-Welch (LZW) Compression | 6 | # Mathematical/analytical approach: Entropy-based redundancy calculation |
| 7 | 7 | ||
| 8 | def compress_lzw(uncompressed): | 8 | from collections import Counter |
| 9 | dictionary = {chr(i): i for i in range(256)} | 9 | from math import log2 |
| 10 | dict_size = 256 | 10 | |
| 11 | w = "" | 11 | def entropy(s): |
| 12 | result = [] | 12 | probabilities = [freq / len(s) for freq in Counter(s).values()] |
| 13 | for c in uncompressed: | 13 | return -sum(p * log2(p) if p > 0 else 0 for p in probabilities) |
| 14 | wc = w + c | 14 | |
| 15 | if wc in dictionary: | 15 | def redundancy(s): |
| 16 | w = wc | 16 | max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 0 |
| 17 | else: | 17 | actual_entropy = entropy(s) |
| 18 | result.append(dictionary[w]) | 18 | return max_entropy - actual_entropy |
| 19 | if dict_size < 4096: # Limit dictionary size for practical use | 19 | |
| 20 | dictionary[wc] = dict_size | 20 | # Calculate reduction in size possible based on redundancy |
| 21 | dict_size += 1 | 21 | reduction_potential = redundancy(data) |
| 22 | w = c | 22 | |
| 23 | if w: | 23 | # Assuming compression is achieved based on redundancy |
| 24 | result.append(dictionary[w]) | 24 | max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data))) |
| 25 | return result | 25 | |
| 26 | 26 | # Qualitative check if max_possible_compression_ratio makes sense | |
| 27 | def decompress_lzw(compressed): | 27 | if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0: |
| 28 | dictionary = {i: chr(i) for i in range(256)} | 28 | return 999.0 |
| 29 | dict_size = 256 | 29 | |
| 30 | w = chr(compressed.pop(0)) | 30 | # Verify compression is lossless (hypothetical check here) |
| 31 | result = [w] | 31 | # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data |
| 32 | for k in compressed: | 32 | |
| 33 | if k in dictionary: | 33 | # Returning the hypothetical compression performance |
| 34 | entry = dictionary[k] | 34 | return max_possible_compression_ratio |
| 35 | elif k == dict_size: | 35 | |
| 36 | entry = w + w[0] | 36 | |
| 37 | else: | 37 | |
| 38 | return None # Decompression failed | 38 | |
| 39 | result.append(entry) | 39 | |
| 40 | 40 | ||
| 41 | if dict_size < 4096: # Limit dictionary size for practical use | 41 | |
| 42 | dictionary[dict_size] = w + entry[0] | 42 | |
| 43 | dict_size += 1 | 43 | |
| 44 | 44 | ||
| 45 | w = entry | 45 | |
| 46 | return ''.join(result) | 46 | |
| 47 | 47 | ||
| 48 | compressed_data = compress_lzw(data) | 48 | |
| 49 | decompressed_data = decompress_lzw(compressed_data) | 49 | |
| 50 | 50 | ||
| 51 | if decompressed_data != data: | 51 | |
| 52 | return 999.0 | 52 | |
| 53 | 53 | ||
| 54 | original_size = len(data) | 54 | |
| 55 | compressed_size = len(compressed_data) * 2 # Assuming 16-bit integers for each entry | 55 | |
| 56 | 56 | ||
| 57 | if original_size == 0: | 57 | |
| 58 | return 999.0 | 58 | |
| 59 | 59 | ||
| 60 | compression_ratio = compressed_size / original_size | 60 | |
| 61 | return 1.0 - compression_ratio | 61 |
1def solve(input):2 data = input.get("data", "")3 if not isinstance(data, str) or not data:4 return 999.056 # Approach: Lempel-Ziv-Welch (LZW) Compression78 def compress_lzw(uncompressed):9 dictionary = {chr(i): i for i in range(256)}10 dict_size = 25611 w = ""12 result = []13 for c in uncompressed:14 wc = w + c15 if wc in dictionary:16 w = wc17 else:18 result.append(dictionary[w])19 if dict_size < 4096: # Limit dictionary size for practical use20 dictionary[wc] = dict_size21 dict_size += 122 w = c23 if w:24 result.append(dictionary[w])25 return result2627 def decompress_lzw(compressed):28 dictionary = {i: chr(i) for i in range(256)}29 dict_size = 25630 w = chr(compressed.pop(0))31 result = [w]32 for k in compressed:33 if k in dictionary:34 entry = dictionary[k]35 elif k == dict_size:36 entry = w + w[0]37 else:38 return None # Decompression failed39 result.append(entry)4041 if dict_size < 4096: # Limit dictionary size for practical use42 dictionary[dict_size] = w + entry[0]43 dict_size += 14445 w = entry46 return ''.join(result)4748 compressed_data = compress_lzw(data)49 decompressed_data = decompress_lzw(compressed_data)5051 if decompressed_data != data:52 return 999.05354 original_size = len(data)55 compressed_size = len(compressed_data) * 2 # Assuming 16-bit integers for each entry5657 if original_size == 0:58 return 999.05960 compression_ratio = compressed_size / original_size61 return 1.0 - compression_ratio1def solve(input):2 data = input.get("data", "")3 if not isinstance(data, str) or not data:4 return 999.056 # Mathematical/analytical approach: Entropy-based redundancy calculation7 8 from collections import Counter9 from math import log21011 def entropy(s):12 probabilities = [freq / len(s) for freq in Counter(s).values()]13 return -sum(p * log2(p) if p > 0 else 0 for p in probabilities)1415 def redundancy(s):16 max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 017 actual_entropy = entropy(s)18 return max_entropy - actual_entropy1920 # Calculate reduction in size possible based on redundancy21 reduction_potential = redundancy(data)2223 # Assuming compression is achieved based on redundancy24 max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data)))25 26 # Qualitative check if max_possible_compression_ratio makes sense27 if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0:28 return 999.02930 # Verify compression is lossless (hypothetical check here)31 # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data32 33 # Returning the hypothetical compression performance34 return max_possible_compression_ratio