Solution #7f265cec-ed83-4dcd-a7b9-e9a40f30fdec
completedScore
45% (0/5)
Runtime
919.94ms
Delta
+135.6% vs parent
-53.2% vs best
Improved from parent
Score
45% (0/5)
Runtime
919.94ms
Delta
+135.6% vs parent
-53.2% vs best
Improved from parent
def solve(input):
try:
data = input.get("data", "") if isinstance(input, dict) else ""
if not isinstance(data, str):
data = str(data)
n = len(data)
if n == 0:
return 0.0
# Novel approach: exact smallest grammar over repeated substrings via memoized recursion,
# using only expression-style comprehensions / higher-order constructs.
# Token costs are abstract character counts for a textual encoding:
# literal substring S -> len(S)
# repetition k * pattern -> len(str(k)) + 2 + cost(pattern) represented like "k(pattern)"
# concatenation -> sum of part costs
memo = {}
def best(s):
if s in memo:
return memo[s]
m = len(s)
if m <= 1:
memo[s] = (m, ("lit", s))
return memo[s]
literal = (m, ("lit", s))
splits = [best(s[:i])[0] + best(s[i:])[0] for i in range(1, m)]
split_best = (min(splits), None) if splits else literal
divisors = [p for p in range(1, m // 2 + 1) if m % p == 0]
reps = [
(len(str(m // p)) + 2 + best(s[:p])[0], ("rep", m // p, best(s[:p])[1]))
for p in divisors
if s == s[:p] * (m // p)
]
rep_best = min(reps, key=lambda x: x[0]) if reps else literal
ans = min([literal, split_best, rep_best], key=lambda x: x[0])
memo[s] = ans
return ans
def decode(node):
kind = node[0]
return (
node[1]
if kind == "lit"
else decode(node[2]) * node[1]
if kind == "rep"
else decode(node[1]) + decode(node[2])
)
# Recompute structure-aware best with stored splits, still using comprehensions only.
memo2 = {}
def build(s):
if s in memo2:
return memo2[s]
m = len(s)
if m <= 1:
memo2[s] = (m, ("lit", s))
return memo2[s]
candidates = [(m, ("lit", s))] + [
(lambda l, r: (l[0] + r[0], ("cat", l[1], r[1])))(build(s[:i]), build(s[i:]))
for i in range(1, m)
] + [
(len(str(m // p)) + 2 + build(s[:p])[0], ("rep", m // p, build(s[:p])[1]))
for p in [d for d in range(1, m // 2 + 1) if m % d == 0]
if s == s[:p] * (m // p)
]
memo2[s] = min(candidates, key=lambda x: x[0])
return memo2[s]
compressed_size, tree = build(data)
def decode2(node):
return (
node[1]
if node[0] == "lit"
else decode2(node[1]) + decode2(node[2])
if node[0] == "cat"
else decode2(node[2]) * node[1]
)
if decode2(tree) != data:
return 999.0
return float(compressed_size) / float(n)
except:
return 999.0Score Difference
-51.4%
Runtime Advantage
919.81ms slower
Code Size
92 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", "") if isinstance(input, dict) 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 | n = len(data) | 6 | # Mathematical/analytical approach: Entropy-based redundancy calculation |
| 7 | if n == 0: | 7 | |
| 8 | return 0.0 | 8 | from collections import Counter |
| 9 | 9 | from math import log2 | |
| 10 | # Novel approach: exact smallest grammar over repeated substrings via memoized recursion, | 10 | |
| 11 | # using only expression-style comprehensions / higher-order constructs. | 11 | def entropy(s): |
| 12 | # Token costs are abstract character counts for a textual encoding: | 12 | probabilities = [freq / len(s) for freq in Counter(s).values()] |
| 13 | # literal substring S -> len(S) | 13 | return -sum(p * log2(p) if p > 0 else 0 for p in probabilities) |
| 14 | # repetition k * pattern -> len(str(k)) + 2 + cost(pattern) represented like "k(pattern)" | 14 | |
| 15 | # concatenation -> sum of part costs | 15 | def redundancy(s): |
| 16 | memo = {} | 16 | max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 0 |
| 17 | 17 | actual_entropy = entropy(s) | |
| 18 | def best(s): | 18 | return max_entropy - actual_entropy |
| 19 | if s in memo: | 19 | |
| 20 | return memo[s] | 20 | # Calculate reduction in size possible based on redundancy |
| 21 | m = len(s) | 21 | reduction_potential = redundancy(data) |
| 22 | if m <= 1: | 22 | |
| 23 | memo[s] = (m, ("lit", s)) | 23 | # Assuming compression is achieved based on redundancy |
| 24 | return memo[s] | 24 | max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data))) |
| 25 | 25 | ||
| 26 | literal = (m, ("lit", s)) | 26 | # Qualitative check if max_possible_compression_ratio makes sense |
| 27 | 27 | if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0: | |
| 28 | splits = [best(s[:i])[0] + best(s[i:])[0] for i in range(1, m)] | 28 | return 999.0 |
| 29 | split_best = (min(splits), None) if splits else literal | 29 | |
| 30 | 30 | # Verify compression is lossless (hypothetical check here) | |
| 31 | divisors = [p for p in range(1, m // 2 + 1) if m % p == 0] | 31 | # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data |
| 32 | reps = [ | 32 | |
| 33 | (len(str(m // p)) + 2 + best(s[:p])[0], ("rep", m // p, best(s[:p])[1])) | 33 | # Returning the hypothetical compression performance |
| 34 | for p in divisors | 34 | return max_possible_compression_ratio |
| 35 | if s == s[:p] * (m // p) | 35 | |
| 36 | ] | 36 | |
| 37 | rep_best = min(reps, key=lambda x: x[0]) if reps else literal | 37 | |
| 38 | 38 | ||
| 39 | ans = min([literal, split_best, rep_best], key=lambda x: x[0]) | 39 | |
| 40 | memo[s] = ans | 40 | |
| 41 | return ans | 41 | |
| 42 | 42 | ||
| 43 | def decode(node): | 43 | |
| 44 | kind = node[0] | 44 | |
| 45 | return ( | 45 | |
| 46 | node[1] | 46 | |
| 47 | if kind == "lit" | 47 | |
| 48 | else decode(node[2]) * node[1] | 48 | |
| 49 | if kind == "rep" | 49 | |
| 50 | else decode(node[1]) + decode(node[2]) | 50 | |
| 51 | ) | 51 | |
| 52 | 52 | ||
| 53 | # Recompute structure-aware best with stored splits, still using comprehensions only. | 53 | |
| 54 | memo2 = {} | 54 | |
| 55 | 55 | ||
| 56 | def build(s): | 56 | |
| 57 | if s in memo2: | 57 | |
| 58 | return memo2[s] | 58 | |
| 59 | m = len(s) | 59 | |
| 60 | if m <= 1: | 60 | |
| 61 | memo2[s] = (m, ("lit", s)) | 61 | |
| 62 | return memo2[s] | 62 | |
| 63 | 63 | ||
| 64 | candidates = [(m, ("lit", s))] + [ | 64 | |
| 65 | (lambda l, r: (l[0] + r[0], ("cat", l[1], r[1])))(build(s[:i]), build(s[i:])) | 65 | |
| 66 | for i in range(1, m) | 66 | |
| 67 | ] + [ | 67 | |
| 68 | (len(str(m // p)) + 2 + build(s[:p])[0], ("rep", m // p, build(s[:p])[1])) | 68 | |
| 69 | for p in [d for d in range(1, m // 2 + 1) if m % d == 0] | 69 | |
| 70 | if s == s[:p] * (m // p) | 70 | |
| 71 | ] | 71 | |
| 72 | 72 | ||
| 73 | memo2[s] = min(candidates, key=lambda x: x[0]) | 73 | |
| 74 | return memo2[s] | 74 | |
| 75 | 75 | ||
| 76 | compressed_size, tree = build(data) | 76 | |
| 77 | 77 | ||
| 78 | def decode2(node): | 78 | |
| 79 | return ( | 79 | |
| 80 | node[1] | 80 | |
| 81 | if node[0] == "lit" | 81 | |
| 82 | else decode2(node[1]) + decode2(node[2]) | 82 | |
| 83 | if node[0] == "cat" | 83 | |
| 84 | else decode2(node[2]) * node[1] | 84 | |
| 85 | ) | 85 | |
| 86 | 86 | ||
| 87 | if decode2(tree) != data: | 87 | |
| 88 | return 999.0 | 88 | |
| 89 | 89 | ||
| 90 | return float(compressed_size) / float(n) | 90 | |
| 91 | except: | 91 | |
| 92 | return 999.0 | 92 |
1def solve(input):2 try:3 data = input.get("data", "") if isinstance(input, dict) else ""4 if not isinstance(data, str):5 data = str(data)6 n = len(data)7 if n == 0:8 return 0.0910 # Novel approach: exact smallest grammar over repeated substrings via memoized recursion,11 # using only expression-style comprehensions / higher-order constructs.12 # Token costs are abstract character counts for a textual encoding:13 # literal substring S -> len(S)14 # repetition k * pattern -> len(str(k)) + 2 + cost(pattern) represented like "k(pattern)"15 # concatenation -> sum of part costs16 memo = {}1718 def best(s):19 if s in memo:20 return memo[s]21 m = len(s)22 if m <= 1:23 memo[s] = (m, ("lit", s))24 return memo[s]2526 literal = (m, ("lit", s))2728 splits = [best(s[:i])[0] + best(s[i:])[0] for i in range(1, m)]29 split_best = (min(splits), None) if splits else literal3031 divisors = [p for p in range(1, m // 2 + 1) if m % p == 0]32 reps = [33 (len(str(m // p)) + 2 + best(s[:p])[0], ("rep", m // p, best(s[:p])[1]))34 for p in divisors35 if s == s[:p] * (m // p)36 ]37 rep_best = min(reps, key=lambda x: x[0]) if reps else literal3839 ans = min([literal, split_best, rep_best], key=lambda x: x[0])40 memo[s] = ans41 return ans4243 def decode(node):44 kind = node[0]45 return (46 node[1]47 if kind == "lit"48 else decode(node[2]) * node[1]49 if kind == "rep"50 else decode(node[1]) + decode(node[2])51 )5253 # Recompute structure-aware best with stored splits, still using comprehensions only.54 memo2 = {}5556 def build(s):57 if s in memo2:58 return memo2[s]59 m = len(s)60 if m <= 1:61 memo2[s] = (m, ("lit", s))62 return memo2[s]6364 candidates = [(m, ("lit", s))] + [65 (lambda l, r: (l[0] + r[0], ("cat", l[1], r[1])))(build(s[:i]), build(s[i:]))66 for i in range(1, m)67 ] + [68 (len(str(m // p)) + 2 + build(s[:p])[0], ("rep", m // p, build(s[:p])[1]))69 for p in [d for d in range(1, m // 2 + 1) if m % d == 0]70 if s == s[:p] * (m // p)71 ]7273 memo2[s] = min(candidates, key=lambda x: x[0])74 return memo2[s]7576 compressed_size, tree = build(data)7778 def decode2(node):79 return (80 node[1]81 if node[0] == "lit"82 else decode2(node[1]) + decode2(node[2])83 if node[0] == "cat"84 else decode2(node[2]) * node[1]85 )8687 if decode2(tree) != data:88 return 999.08990 return float(compressed_size) / float(n)91 except:92 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