Solution #dd5155da-49f0-4653-8006-59d5275fd368
completedScore
19% (0/5)
Runtime
195μs
Delta
New score
-80.1% vs best
Improved from parent
Score
19% (0/5)
Runtime
195μs
Delta
New score
-80.1% vs best
Improved from parent
def solve(input):
try:
data = input["data"] if isinstance(input, dict) and "data" in input else ""
if not isinstance(data, str):
data = str(data)
n = len(data)
if n == 0:
return 0.0
def gen_rle_encode(s):
it = iter(s)
try:
prev = next(it)
except StopIteration:
return
cnt = 1
for ch in it:
if ch == prev:
cnt += 1
else:
yield (prev, cnt)
prev = ch
cnt = 1
yield (prev, cnt)
def gen_rle_decode(tokens):
for ch, cnt in tokens:
for _ in range(cnt):
yield ch
def verify_rle(s):
tokens = list(gen_rle_encode(s))
decoded = "".join(gen_rle_decode(tokens))
if decoded != s:
return None
return tokens
tokens = verify_rle(data)
if tokens is None:
return 999.0
compressed_size = sum(1 + len(str(cnt)) for _, cnt in tokens)
return float(compressed_size / n)
except:
return 999.0Score Difference
-77.4%
Runtime Advantage
65μs slower
Code Size
46 vs 34 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | try: | 2 | data = input.get("data", "") |
| 3 | data = input["data"] if isinstance(input, dict) and "data" in input else "" | 3 | if not isinstance(data, str) or not data: |
| 4 | if not isinstance(data, str): | 4 | return 999.0 |
| 5 | data = str(data) | 5 | |
| 6 | 6 | # Mathematical/analytical approach: Entropy-based redundancy calculation | |
| 7 | n = len(data) | 7 | |
| 8 | if n == 0: | 8 | from collections import Counter |
| 9 | return 0.0 | 9 | from math import log2 |
| 10 | 10 | ||
| 11 | def gen_rle_encode(s): | 11 | def entropy(s): |
| 12 | it = iter(s) | 12 | probabilities = [freq / len(s) for freq in Counter(s).values()] |
| 13 | try: | 13 | return -sum(p * log2(p) if p > 0 else 0 for p in probabilities) |
| 14 | prev = next(it) | 14 | |
| 15 | except StopIteration: | 15 | def redundancy(s): |
| 16 | return | 16 | max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 0 |
| 17 | cnt = 1 | 17 | actual_entropy = entropy(s) |
| 18 | for ch in it: | 18 | return max_entropy - actual_entropy |
| 19 | if ch == prev: | 19 | |
| 20 | cnt += 1 | 20 | # Calculate reduction in size possible based on redundancy |
| 21 | else: | 21 | reduction_potential = redundancy(data) |
| 22 | yield (prev, cnt) | 22 | |
| 23 | prev = ch | 23 | # Assuming compression is achieved based on redundancy |
| 24 | cnt = 1 | 24 | max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data))) |
| 25 | yield (prev, cnt) | 25 | |
| 26 | 26 | # Qualitative check if max_possible_compression_ratio makes sense | |
| 27 | def gen_rle_decode(tokens): | 27 | if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0: |
| 28 | for ch, cnt in tokens: | 28 | return 999.0 |
| 29 | for _ in range(cnt): | 29 | |
| 30 | yield ch | 30 | # Verify compression is lossless (hypothetical check here) |
| 31 | 31 | # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data | |
| 32 | def verify_rle(s): | 32 | |
| 33 | tokens = list(gen_rle_encode(s)) | 33 | # Returning the hypothetical compression performance |
| 34 | decoded = "".join(gen_rle_decode(tokens)) | 34 | return max_possible_compression_ratio |
| 35 | if decoded != s: | 35 | |
| 36 | return None | 36 | |
| 37 | return tokens | 37 | |
| 38 | 38 | ||
| 39 | tokens = verify_rle(data) | 39 | |
| 40 | if tokens is None: | 40 | |
| 41 | return 999.0 | 41 | |
| 42 | 42 | ||
| 43 | compressed_size = sum(1 + len(str(cnt)) for _, cnt in tokens) | 43 | |
| 44 | return float(compressed_size / n) | 44 | |
| 45 | except: | 45 | |
| 46 | return 999.0 | 46 |
1def solve(input):2 try:3 data = input["data"] if isinstance(input, dict) and "data" in input else ""4 if not isinstance(data, str):5 data = str(data)67 n = len(data)8 if n == 0:9 return 0.01011 def gen_rle_encode(s):12 it = iter(s)13 try:14 prev = next(it)15 except StopIteration:16 return17 cnt = 118 for ch in it:19 if ch == prev:20 cnt += 121 else:22 yield (prev, cnt)23 prev = ch24 cnt = 125 yield (prev, cnt)2627 def gen_rle_decode(tokens):28 for ch, cnt in tokens:29 for _ in range(cnt):30 yield ch3132 def verify_rle(s):33 tokens = list(gen_rle_encode(s))34 decoded = "".join(gen_rle_decode(tokens))35 if decoded != s:36 return None37 return tokens3839 tokens = verify_rle(data)40 if tokens is None:41 return 999.04243 compressed_size = sum(1 + len(str(cnt)) for _, cnt in tokens)44 return float(compressed_size / n)45 except:46 return 999.01def 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