Solution #2c8087b0-079f-4f10-a068-d4b09f60d2bc
completedScore
20% (0/5)
Runtime
127μs
Delta
-2.6% vs parent
-79.7% vs best
Regression from parent
Score
20% (0/5)
Runtime
127μs
Delta
-2.6% vs parent
-79.7% vs best
Regression from parent
def solve(input):
data = input.get("data", "")
if not isinstance(data, str) or not data:
return 999.0
# Approach: Dynamic Programming with Run-length Encoding
def compress_rle(s):
if not s:
return []
compressed = []
current_char = s[0]
current_count = 1
for char in s[1:]:
if char == current_char:
current_count += 1
else:
compressed.append((current_char, current_count))
current_char = char
current_count = 1
compressed.append((current_char, current_count))
return compressed
def decompress_rle(compressed):
decompressed = []
for char, count in compressed:
decompressed.append(char * count)
return ''.join(decompressed)
compressed_data = compress_rle(data)
decompressed_data = decompress_rle(compressed_data)
if decompressed_data != data:
return 999.0
original_size = len(data)
compressed_size = sum(len(char) + 1 for char, _ in compressed_data) # Assuming 1 byte for count
if original_size == 0:
return 999.0
compression_ratio = compressed_size / original_size
return 1.0 - compression_ratioScore Difference
-77.0%
Runtime Advantage
3μs faster
Code Size
43 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: Dynamic Programming with Run-length Encoding | 6 | # Mathematical/analytical approach: Entropy-based redundancy calculation |
| 7 | 7 | ||
| 8 | def compress_rle(s): | 8 | from collections import Counter |
| 9 | if not s: | 9 | from math import log2 |
| 10 | return [] | 10 | |
| 11 | compressed = [] | 11 | def entropy(s): |
| 12 | current_char = s[0] | 12 | probabilities = [freq / len(s) for freq in Counter(s).values()] |
| 13 | current_count = 1 | 13 | return -sum(p * log2(p) if p > 0 else 0 for p in probabilities) |
| 14 | for char in s[1:]: | 14 | |
| 15 | if char == current_char: | 15 | def redundancy(s): |
| 16 | current_count += 1 | 16 | max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 0 |
| 17 | else: | 17 | actual_entropy = entropy(s) |
| 18 | compressed.append((current_char, current_count)) | 18 | return max_entropy - actual_entropy |
| 19 | current_char = char | 19 | |
| 20 | current_count = 1 | 20 | # Calculate reduction in size possible based on redundancy |
| 21 | compressed.append((current_char, current_count)) | 21 | reduction_potential = redundancy(data) |
| 22 | return compressed | 22 | |
| 23 | 23 | # Assuming compression is achieved based on redundancy | |
| 24 | def decompress_rle(compressed): | 24 | max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data))) |
| 25 | decompressed = [] | 25 | |
| 26 | for char, count in compressed: | 26 | # Qualitative check if max_possible_compression_ratio makes sense |
| 27 | decompressed.append(char * count) | 27 | if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0: |
| 28 | return ''.join(decompressed) | 28 | return 999.0 |
| 29 | 29 | ||
| 30 | compressed_data = compress_rle(data) | 30 | # Verify compression is lossless (hypothetical check here) |
| 31 | decompressed_data = decompress_rle(compressed_data) | 31 | # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data |
| 32 | 32 | ||
| 33 | if decompressed_data != data: | 33 | # Returning the hypothetical compression performance |
| 34 | return 999.0 | 34 | return max_possible_compression_ratio |
| 35 | 35 | ||
| 36 | original_size = len(data) | 36 | |
| 37 | compressed_size = sum(len(char) + 1 for char, _ in compressed_data) # Assuming 1 byte for count | 37 | |
| 38 | 38 | ||
| 39 | if original_size == 0: | 39 | |
| 40 | return 999.0 | 40 | |
| 41 | 41 | ||
| 42 | compression_ratio = compressed_size / original_size | 42 | |
| 43 | return 1.0 - compression_ratio | 43 |
1def solve(input):2 data = input.get("data", "")3 if not isinstance(data, str) or not data:4 return 999.056 # Approach: Dynamic Programming with Run-length Encoding78 def compress_rle(s):9 if not s:10 return []11 compressed = []12 current_char = s[0]13 current_count = 114 for char in s[1:]:15 if char == current_char:16 current_count += 117 else:18 compressed.append((current_char, current_count))19 current_char = char20 current_count = 121 compressed.append((current_char, current_count))22 return compressed2324 def decompress_rle(compressed):25 decompressed = []26 for char, count in compressed:27 decompressed.append(char * count)28 return ''.join(decompressed)2930 compressed_data = compress_rle(data)31 decompressed_data = decompress_rle(compressed_data)3233 if decompressed_data != data:34 return 999.03536 original_size = len(data)37 compressed_size = sum(len(char) + 1 for char, _ in compressed_data) # Assuming 1 byte for count3839 if original_size == 0:40 return 999.04142 compression_ratio = compressed_size / original_size43 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