Solution #ce53db51-8871-45bf-b887-341b399dad3e
completedScore
52% (0/5)
Runtime
15.09ms
Delta
+23.7% vs parent
-45.9% vs best
Improved from parent
Score
52% (0/5)
Runtime
15.09ms
Delta
+23.7% vs parent
-45.9% vs best
Improved from parent
def solve(input):
try:
data = input.get("data", "")
if not isinstance(data, str):
data = str(data)
n = len(data)
if n == 0:
return 0.0
def vlen(x):
c = 1
while x >= 128:
x >>= 7
c += 1
return c
def find_period(s):
m = len(s)
if m <= 1:
return None
pi = [0] * m
j = 0
for i in range(1, m):
while j and s[i] != s[j]:
j = pi[j - 1]
if s[i] == s[j]:
j += 1
pi[i] = j
p = m - pi[-1]
if p < m and m % p == 0:
return p, m // p
return None
def trie_lz_compress(s):
m = len(s)
if m == 0:
return []
max_len = 64
max_back = 4095
min_match = 3
root = {}
positions = []
def add_suffix(pos):
node = root
end = min(m, pos + max_len)
for j in range(pos, end):
ch = s[j]
nxt = node.get(ch)
if nxt is None:
nxt = {}
node[ch] = nxt
lst = nxt.get("_")
if lst is None:
nxt["_"] = [pos]
else:
lst.append(pos)
if len(lst) > 8:
del lst[0]
node = nxt
for i in range(m):
add_suffix(i)
tokens = []
i = 0
while i < m:
best_len = 0
best_off = 0
node = root
j = i
while j < m and j < i + max_len:
ch = s[j]
node = node.get(ch)
if node is None:
break
cand = node.get("_", [])
if cand:
for p in reversed(cand):
if p >= i:
continue
off = i - p
if off > max_back:
continue
ln = j - i + 1
if ln > best_len:
best_len = ln
best_off = off
j += 1
if best_len >= min_match:
tokens.append(("M", best_off, best_len))
i += best_len
else:
start = i
i += 1
while i < m:
best_len = 0
node = root
j = i
while j < m and j < i + max_len:
ch = s[j]
node = node.get(ch)
if node is None:
break
cand = node.get("_", [])
if cand:
found = False
for p in reversed(cand):
if p < i and i - p <= max_back:
best_len = j - i + 1
found = True
break
if found and best_len >= min_match:
break
j += 1
if best_len >= min_match:
break
i += 1
tokens.append(("L", s[start:i]))
return tokens
def trie_lz_decompress(tokens):
out = []
for t in tokens:
if t[0] == "L":
out.extend(t[1])
else:
off, ln = t[1], t[2]
if off <= 0 or off > len(out):
return None
start = len(out) - off
for k in range(ln):
out.append(out[start + k])
return "".join(out)
def trie_lz_cost(tokens):
c = 0
for t in tokens:
if t[0] == "L":
ln = len(t[1])
c += 1 + vlen(ln) + ln
else:
c += 1 + vlen(t[1]) + vlen(t[2])
return c
best_ratio = 1.0
p = find_period(data)
if p is not None:
plen, reps = p
if data[:plen] * reps == data:
ratio = (1 + vlen(plen) + plen + vlen(reps)) / n
if ratio < best_ratio:
best_ratio = ratio
runs = []
i = 0
while i < n:
j = i + 1
while j < n and data[j] == data[i]:
j += 1
runs.append((data[i], j - i))
i = j
rle_cost = 0
ok = True
rebuilt = []
for ch, cnt in runs:
rebuilt.append(ch * cnt)
rle_cost += 1 + vlen(cnt)
if "".join(rebuilt) == data:
ratio = rle_cost / n
if ratio < best_ratio:
best_ratio = ratio
else:
ok = False
tokens = trie_lz_compress(data)
dec = trie_lz_decompress(tokens)
if dec != data:
return 999.0
ratio = trie_lz_cost(tokens) / n
if ratio < best_ratio:
best_ratio = ratio
return float(best_ratio)
except:
return 999.0Score Difference
-44.3%
Runtime Advantage
14.96ms slower
Code Size
192 vs 34 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | try: | 2 | data = input.get("data", "") |
| 3 | data = input.get("data", "") | 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 vlen(x): | 11 | def entropy(s): |
| 12 | c = 1 | 12 | probabilities = [freq / len(s) for freq in Counter(s).values()] |
| 13 | while x >= 128: | 13 | return -sum(p * log2(p) if p > 0 else 0 for p in probabilities) |
| 14 | x >>= 7 | 14 | |
| 15 | c += 1 | 15 | def redundancy(s): |
| 16 | return c | 16 | max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 0 |
| 17 | 17 | actual_entropy = entropy(s) | |
| 18 | def find_period(s): | 18 | return max_entropy - actual_entropy |
| 19 | m = len(s) | 19 | |
| 20 | if m <= 1: | 20 | # Calculate reduction in size possible based on redundancy |
| 21 | return None | 21 | reduction_potential = redundancy(data) |
| 22 | pi = [0] * m | 22 | |
| 23 | j = 0 | 23 | # Assuming compression is achieved based on redundancy |
| 24 | for i in range(1, m): | 24 | max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data))) |
| 25 | while j and s[i] != s[j]: | 25 | |
| 26 | j = pi[j - 1] | 26 | # Qualitative check if max_possible_compression_ratio makes sense |
| 27 | if s[i] == s[j]: | 27 | if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0: |
| 28 | j += 1 | 28 | return 999.0 |
| 29 | pi[i] = j | 29 | |
| 30 | p = m - pi[-1] | 30 | # Verify compression is lossless (hypothetical check here) |
| 31 | if p < m and m % p == 0: | 31 | # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data |
| 32 | return p, m // p | 32 | |
| 33 | return None | 33 | # Returning the hypothetical compression performance |
| 34 | 34 | return max_possible_compression_ratio | |
| 35 | def trie_lz_compress(s): | 35 | |
| 36 | m = len(s) | 36 | |
| 37 | if m == 0: | 37 | |
| 38 | return [] | 38 | |
| 39 | 39 | ||
| 40 | max_len = 64 | 40 | |
| 41 | max_back = 4095 | 41 | |
| 42 | min_match = 3 | 42 | |
| 43 | 43 | ||
| 44 | root = {} | 44 | |
| 45 | positions = [] | 45 | |
| 46 | 46 | ||
| 47 | def add_suffix(pos): | 47 | |
| 48 | node = root | 48 | |
| 49 | end = min(m, pos + max_len) | 49 | |
| 50 | for j in range(pos, end): | 50 | |
| 51 | ch = s[j] | 51 | |
| 52 | nxt = node.get(ch) | 52 | |
| 53 | if nxt is None: | 53 | |
| 54 | nxt = {} | 54 | |
| 55 | node[ch] = nxt | 55 | |
| 56 | lst = nxt.get("_") | 56 | |
| 57 | if lst is None: | 57 | |
| 58 | nxt["_"] = [pos] | 58 | |
| 59 | else: | 59 | |
| 60 | lst.append(pos) | 60 | |
| 61 | if len(lst) > 8: | 61 | |
| 62 | del lst[0] | 62 | |
| 63 | node = nxt | 63 | |
| 64 | 64 | ||
| 65 | for i in range(m): | 65 | |
| 66 | add_suffix(i) | 66 | |
| 67 | 67 | ||
| 68 | tokens = [] | 68 | |
| 69 | i = 0 | 69 | |
| 70 | while i < m: | 70 | |
| 71 | best_len = 0 | 71 | |
| 72 | best_off = 0 | 72 | |
| 73 | node = root | 73 | |
| 74 | j = i | 74 | |
| 75 | while j < m and j < i + max_len: | 75 | |
| 76 | ch = s[j] | 76 | |
| 77 | node = node.get(ch) | 77 | |
| 78 | if node is None: | 78 | |
| 79 | break | 79 | |
| 80 | cand = node.get("_", []) | 80 | |
| 81 | if cand: | 81 | |
| 82 | for p in reversed(cand): | 82 | |
| 83 | if p >= i: | 83 | |
| 84 | continue | 84 | |
| 85 | off = i - p | 85 | |
| 86 | if off > max_back: | 86 | |
| 87 | continue | 87 | |
| 88 | ln = j - i + 1 | 88 | |
| 89 | if ln > best_len: | 89 | |
| 90 | best_len = ln | 90 | |
| 91 | best_off = off | 91 | |
| 92 | j += 1 | 92 | |
| 93 | 93 | ||
| 94 | if best_len >= min_match: | 94 | |
| 95 | tokens.append(("M", best_off, best_len)) | 95 | |
| 96 | i += best_len | 96 | |
| 97 | else: | 97 | |
| 98 | start = i | 98 | |
| 99 | i += 1 | 99 | |
| 100 | while i < m: | 100 | |
| 101 | best_len = 0 | 101 | |
| 102 | node = root | 102 | |
| 103 | j = i | 103 | |
| 104 | while j < m and j < i + max_len: | 104 | |
| 105 | ch = s[j] | 105 | |
| 106 | node = node.get(ch) | 106 | |
| 107 | if node is None: | 107 | |
| 108 | break | 108 | |
| 109 | cand = node.get("_", []) | 109 | |
| 110 | if cand: | 110 | |
| 111 | found = False | 111 | |
| 112 | for p in reversed(cand): | 112 | |
| 113 | if p < i and i - p <= max_back: | 113 | |
| 114 | best_len = j - i + 1 | 114 | |
| 115 | found = True | 115 | |
| 116 | break | 116 | |
| 117 | if found and best_len >= min_match: | 117 | |
| 118 | break | 118 | |
| 119 | j += 1 | 119 | |
| 120 | if best_len >= min_match: | 120 | |
| 121 | break | 121 | |
| 122 | i += 1 | 122 | |
| 123 | tokens.append(("L", s[start:i])) | 123 | |
| 124 | return tokens | 124 | |
| 125 | 125 | ||
| 126 | def trie_lz_decompress(tokens): | 126 | |
| 127 | out = [] | 127 | |
| 128 | for t in tokens: | 128 | |
| 129 | if t[0] == "L": | 129 | |
| 130 | out.extend(t[1]) | 130 | |
| 131 | else: | 131 | |
| 132 | off, ln = t[1], t[2] | 132 | |
| 133 | if off <= 0 or off > len(out): | 133 | |
| 134 | return None | 134 | |
| 135 | start = len(out) - off | 135 | |
| 136 | for k in range(ln): | 136 | |
| 137 | out.append(out[start + k]) | 137 | |
| 138 | return "".join(out) | 138 | |
| 139 | 139 | ||
| 140 | def trie_lz_cost(tokens): | 140 | |
| 141 | c = 0 | 141 | |
| 142 | for t in tokens: | 142 | |
| 143 | if t[0] == "L": | 143 | |
| 144 | ln = len(t[1]) | 144 | |
| 145 | c += 1 + vlen(ln) + ln | 145 | |
| 146 | else: | 146 | |
| 147 | c += 1 + vlen(t[1]) + vlen(t[2]) | 147 | |
| 148 | return c | 148 | |
| 149 | 149 | ||
| 150 | best_ratio = 1.0 | 150 | |
| 151 | 151 | ||
| 152 | p = find_period(data) | 152 | |
| 153 | if p is not None: | 153 | |
| 154 | plen, reps = p | 154 | |
| 155 | if data[:plen] * reps == data: | 155 | |
| 156 | ratio = (1 + vlen(plen) + plen + vlen(reps)) / n | 156 | |
| 157 | if ratio < best_ratio: | 157 | |
| 158 | best_ratio = ratio | 158 | |
| 159 | 159 | ||
| 160 | runs = [] | 160 | |
| 161 | i = 0 | 161 | |
| 162 | while i < n: | 162 | |
| 163 | j = i + 1 | 163 | |
| 164 | while j < n and data[j] == data[i]: | 164 | |
| 165 | j += 1 | 165 | |
| 166 | runs.append((data[i], j - i)) | 166 | |
| 167 | i = j | 167 | |
| 168 | 168 | ||
| 169 | rle_cost = 0 | 169 | |
| 170 | ok = True | 170 | |
| 171 | rebuilt = [] | 171 | |
| 172 | for ch, cnt in runs: | 172 | |
| 173 | rebuilt.append(ch * cnt) | 173 | |
| 174 | rle_cost += 1 + vlen(cnt) | 174 | |
| 175 | if "".join(rebuilt) == data: | 175 | |
| 176 | ratio = rle_cost / n | 176 | |
| 177 | if ratio < best_ratio: | 177 | |
| 178 | best_ratio = ratio | 178 | |
| 179 | else: | 179 | |
| 180 | ok = False | 180 | |
| 181 | 181 | ||
| 182 | tokens = trie_lz_compress(data) | 182 | |
| 183 | dec = trie_lz_decompress(tokens) | 183 | |
| 184 | if dec != data: | 184 | |
| 185 | return 999.0 | 185 | |
| 186 | ratio = trie_lz_cost(tokens) / n | 186 | |
| 187 | if ratio < best_ratio: | 187 | |
| 188 | best_ratio = ratio | 188 | |
| 189 | 189 | ||
| 190 | return float(best_ratio) | 190 | |
| 191 | except: | 191 | |
| 192 | return 999.0 | 192 |
1def solve(input):2 try:3 data = input.get("data", "")4 if not isinstance(data, str):5 data = str(data)67 n = len(data)8 if n == 0:9 return 0.01011 def vlen(x):12 c = 113 while x >= 128:14 x >>= 715 c += 116 return c1718 def find_period(s):19 m = len(s)20 if m <= 1:21 return None22 pi = [0] * m23 j = 024 for i in range(1, m):25 while j and s[i] != s[j]:26 j = pi[j - 1]27 if s[i] == s[j]:28 j += 129 pi[i] = j30 p = m - pi[-1]31 if p < m and m % p == 0:32 return p, m // p33 return None3435 def trie_lz_compress(s):36 m = len(s)37 if m == 0:38 return []3940 max_len = 6441 max_back = 409542 min_match = 34344 root = {}45 positions = []4647 def add_suffix(pos):48 node = root49 end = min(m, pos + max_len)50 for j in range(pos, end):51 ch = s[j]52 nxt = node.get(ch)53 if nxt is None:54 nxt = {}55 node[ch] = nxt56 lst = nxt.get("_")57 if lst is None:58 nxt["_"] = [pos]59 else:60 lst.append(pos)61 if len(lst) > 8:62 del lst[0]63 node = nxt6465 for i in range(m):66 add_suffix(i)6768 tokens = []69 i = 070 while i < m:71 best_len = 072 best_off = 073 node = root74 j = i75 while j < m and j < i + max_len:76 ch = s[j]77 node = node.get(ch)78 if node is None:79 break80 cand = node.get("_", [])81 if cand:82 for p in reversed(cand):83 if p >= i:84 continue85 off = i - p86 if off > max_back:87 continue88 ln = j - i + 189 if ln > best_len:90 best_len = ln91 best_off = off92 j += 19394 if best_len >= min_match:95 tokens.append(("M", best_off, best_len))96 i += best_len97 else:98 start = i99 i += 1100 while i < m:101 best_len = 0102 node = root103 j = i104 while j < m and j < i + max_len:105 ch = s[j]106 node = node.get(ch)107 if node is None:108 break109 cand = node.get("_", [])110 if cand:111 found = False112 for p in reversed(cand):113 if p < i and i - p <= max_back:114 best_len = j - i + 1115 found = True116 break117 if found and best_len >= min_match:118 break119 j += 1120 if best_len >= min_match:121 break122 i += 1123 tokens.append(("L", s[start:i]))124 return tokens125126 def trie_lz_decompress(tokens):127 out = []128 for t in tokens:129 if t[0] == "L":130 out.extend(t[1])131 else:132 off, ln = t[1], t[2]133 if off <= 0 or off > len(out):134 return None135 start = len(out) - off136 for k in range(ln):137 out.append(out[start + k])138 return "".join(out)139140 def trie_lz_cost(tokens):141 c = 0142 for t in tokens:143 if t[0] == "L":144 ln = len(t[1])145 c += 1 + vlen(ln) + ln146 else:147 c += 1 + vlen(t[1]) + vlen(t[2])148 return c149150 best_ratio = 1.0151152 p = find_period(data)153 if p is not None:154 plen, reps = p155 if data[:plen] * reps == data:156 ratio = (1 + vlen(plen) + plen + vlen(reps)) / n157 if ratio < best_ratio:158 best_ratio = ratio159160 runs = []161 i = 0162 while i < n:163 j = i + 1164 while j < n and data[j] == data[i]:165 j += 1166 runs.append((data[i], j - i))167 i = j168169 rle_cost = 0170 ok = True171 rebuilt = []172 for ch, cnt in runs:173 rebuilt.append(ch * cnt)174 rle_cost += 1 + vlen(cnt)175 if "".join(rebuilt) == data:176 ratio = rle_cost / n177 if ratio < best_ratio:178 best_ratio = ratio179 else:180 ok = False181182 tokens = trie_lz_compress(data)183 dec = trie_lz_decompress(tokens)184 if dec != data:185 return 999.0186 ratio = trie_lz_cost(tokens) / n187 if ratio < best_ratio:188 best_ratio = ratio189190 return float(best_ratio)191 except:192 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