Solution #bf75b8b7-8dfe-4bad-b9b9-bc78a95ee3a2
completedScore
41% (0/5)
Runtime
2.45ms
Delta
+48.6% vs parent
-57.4% vs best
Improved from parent
Score
41% (0/5)
Runtime
2.45ms
Delta
+48.6% vs parent
-57.4% vs best
Improved from parent
def solve(input):
from collections import defaultdict
data = input.get("data", "")
if not isinstance(data, str) or not data:
return 999.0
# Implementing a Huffman coding algorithm
class Node:
def __init__(self, char, freq):
self.char = char
self.freq = freq
self.left = None
self.right = None
def __lt__(self, other):
return self.freq < other.freq
def build_huffman_tree(text):
frequency = defaultdict(int)
for char in text:
frequency[char] += 1
heap = [Node(char, freq) for char, freq in frequency.items()]
heap.sort(reverse=True)
while len(heap) > 1:
left = heap.pop()
right = heap.pop()
merged = Node(None, left.freq + right.freq)
merged.left = left
merged.right = right
heap.append(merged)
heap.sort(reverse=True)
return heap[0]
def build_codes(node, current_code, codes):
if node is None:
return
if node.char is not None:
codes[node.char] = current_code
build_codes(node.left, current_code + "0", codes)
build_codes(node.right, current_code + "1", codes)
def huffman_compress(text):
root = build_huffman_tree(text)
codes = {}
build_codes(root, "", codes)
compressed_data = "".join(codes[char] for char in text)
return compressed_data, root
def huffman_decompress(compressed_data, root):
decompressed_data = []
current_node = root
for bit in compressed_data:
if bit == '0':
current_node = current_node.left
else:
current_node = current_node.right
if current_node.char is not None:
decompressed_data.append(current_node.char)
current_node = root
return ''.join(decompressed_data)
# Compress and Decompress
compressed_data, huffman_tree = huffman_compress(data)
decompressed_data = huffman_decompress(compressed_data, huffman_tree)
if decompressed_data != data:
return 999.0
original_size = len(data) * 8
compressed_size = len(compressed_data)
if original_size == 0:
return 999.0
compression_ratio = compressed_size / original_size
return 1.0 - compression_ratioScore Difference
-55.4%
Runtime Advantage
2.32ms slower
Code Size
85 vs 34 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | from collections import defaultdict | 2 | data = input.get("data", "") |
| 3 | 3 | if not isinstance(data, str) or not data: | |
| 4 | data = input.get("data", "") | 4 | return 999.0 |
| 5 | if not isinstance(data, str) or not data: | 5 | |
| 6 | return 999.0 | 6 | # Mathematical/analytical approach: Entropy-based redundancy calculation |
| 7 | 7 | ||
| 8 | # Implementing a Huffman coding algorithm | 8 | from collections import Counter |
| 9 | class Node: | 9 | from math import log2 |
| 10 | def __init__(self, char, freq): | 10 | |
| 11 | self.char = char | 11 | def entropy(s): |
| 12 | self.freq = freq | 12 | probabilities = [freq / len(s) for freq in Counter(s).values()] |
| 13 | self.left = None | 13 | return -sum(p * log2(p) if p > 0 else 0 for p in probabilities) |
| 14 | self.right = None | 14 | |
| 15 | 15 | def redundancy(s): | |
| 16 | def __lt__(self, other): | 16 | max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 0 |
| 17 | return self.freq < other.freq | 17 | actual_entropy = entropy(s) |
| 18 | 18 | return max_entropy - actual_entropy | |
| 19 | def build_huffman_tree(text): | 19 | |
| 20 | frequency = defaultdict(int) | 20 | # Calculate reduction in size possible based on redundancy |
| 21 | for char in text: | 21 | reduction_potential = redundancy(data) |
| 22 | frequency[char] += 1 | 22 | |
| 23 | 23 | # Assuming compression is achieved based on redundancy | |
| 24 | heap = [Node(char, freq) for char, freq in frequency.items()] | 24 | max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data))) |
| 25 | heap.sort(reverse=True) | 25 | |
| 26 | 26 | # Qualitative check if max_possible_compression_ratio makes sense | |
| 27 | while len(heap) > 1: | 27 | if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0: |
| 28 | left = heap.pop() | 28 | return 999.0 |
| 29 | right = heap.pop() | 29 | |
| 30 | merged = Node(None, left.freq + right.freq) | 30 | # Verify compression is lossless (hypothetical check here) |
| 31 | merged.left = left | 31 | # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data |
| 32 | merged.right = right | 32 | |
| 33 | heap.append(merged) | 33 | # Returning the hypothetical compression performance |
| 34 | heap.sort(reverse=True) | 34 | return max_possible_compression_ratio |
| 35 | 35 | ||
| 36 | return heap[0] | 36 | |
| 37 | 37 | ||
| 38 | def build_codes(node, current_code, codes): | 38 | |
| 39 | if node is None: | 39 | |
| 40 | return | 40 | |
| 41 | 41 | ||
| 42 | if node.char is not None: | 42 | |
| 43 | codes[node.char] = current_code | 43 | |
| 44 | 44 | ||
| 45 | build_codes(node.left, current_code + "0", codes) | 45 | |
| 46 | build_codes(node.right, current_code + "1", codes) | 46 | |
| 47 | 47 | ||
| 48 | def huffman_compress(text): | 48 | |
| 49 | root = build_huffman_tree(text) | 49 | |
| 50 | codes = {} | 50 | |
| 51 | build_codes(root, "", codes) | 51 | |
| 52 | 52 | ||
| 53 | compressed_data = "".join(codes[char] for char in text) | 53 | |
| 54 | return compressed_data, root | 54 | |
| 55 | 55 | ||
| 56 | def huffman_decompress(compressed_data, root): | 56 | |
| 57 | decompressed_data = [] | 57 | |
| 58 | current_node = root | 58 | |
| 59 | for bit in compressed_data: | 59 | |
| 60 | if bit == '0': | 60 | |
| 61 | current_node = current_node.left | 61 | |
| 62 | else: | 62 | |
| 63 | current_node = current_node.right | 63 | |
| 64 | 64 | ||
| 65 | if current_node.char is not None: | 65 | |
| 66 | decompressed_data.append(current_node.char) | 66 | |
| 67 | current_node = root | 67 | |
| 68 | 68 | ||
| 69 | return ''.join(decompressed_data) | 69 | |
| 70 | 70 | ||
| 71 | # Compress and Decompress | 71 | |
| 72 | compressed_data, huffman_tree = huffman_compress(data) | 72 | |
| 73 | decompressed_data = huffman_decompress(compressed_data, huffman_tree) | 73 | |
| 74 | 74 | ||
| 75 | if decompressed_data != data: | 75 | |
| 76 | return 999.0 | 76 | |
| 77 | 77 | ||
| 78 | original_size = len(data) * 8 | 78 | |
| 79 | compressed_size = len(compressed_data) | 79 | |
| 80 | 80 | ||
| 81 | if original_size == 0: | 81 | |
| 82 | return 999.0 | 82 | |
| 83 | 83 | ||
| 84 | compression_ratio = compressed_size / original_size | 84 | |
| 85 | return 1.0 - compression_ratio | 85 |
1def solve(input):2 from collections import defaultdict34 data = input.get("data", "")5 if not isinstance(data, str) or not data:6 return 999.078 # Implementing a Huffman coding algorithm9 class Node:10 def __init__(self, char, freq):11 self.char = char12 self.freq = freq13 self.left = None14 self.right = None15 16 def __lt__(self, other):17 return self.freq < other.freq1819 def build_huffman_tree(text):20 frequency = defaultdict(int)21 for char in text:22 frequency[char] += 123 24 heap = [Node(char, freq) for char, freq in frequency.items()]25 heap.sort(reverse=True)26 27 while len(heap) > 1:28 left = heap.pop()29 right = heap.pop()30 merged = Node(None, left.freq + right.freq)31 merged.left = left32 merged.right = right33 heap.append(merged)34 heap.sort(reverse=True)35 36 return heap[0]3738 def build_codes(node, current_code, codes):39 if node is None:40 return41 42 if node.char is not None:43 codes[node.char] = current_code44 45 build_codes(node.left, current_code + "0", codes)46 build_codes(node.right, current_code + "1", codes)4748 def huffman_compress(text):49 root = build_huffman_tree(text)50 codes = {}51 build_codes(root, "", codes)52 53 compressed_data = "".join(codes[char] for char in text)54 return compressed_data, root5556 def huffman_decompress(compressed_data, root):57 decompressed_data = []58 current_node = root59 for bit in compressed_data:60 if bit == '0':61 current_node = current_node.left62 else:63 current_node = current_node.right64 65 if current_node.char is not None:66 decompressed_data.append(current_node.char)67 current_node = root68 69 return ''.join(decompressed_data)7071 # Compress and Decompress72 compressed_data, huffman_tree = huffman_compress(data)73 decompressed_data = huffman_decompress(compressed_data, huffman_tree)7475 if decompressed_data != data:76 return 999.07778 original_size = len(data) * 879 compressed_size = len(compressed_data)8081 if original_size == 0:82 return 999.08384 compression_ratio = compressed_size / original_size85 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