Solution #ffe6e932-1a78-4af2-bd47-94048be8e1ca
completedScore
100% (5/5)
Runtime
54μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
54μs
Delta
No change vs parent
Tied for best
Same as parent
def solve(input):
text = input.get("text", "")
n = len(text)
if n == 0:
return {"encoded_length": 0, "decoded": ""}
try:
from bisect import insort
except:
def insort(a, x):
lo, hi = 0, len(a)
while lo < hi:
mid = (lo + hi) >> 1
if a[mid] <= x:
lo = mid + 1
else:
hi = mid
a.insert(lo, x)
try:
counts = [0] * 256
for ch in text:
counts[ord(ch)] += 1
freqs = []
for c in counts:
if c:
freqs.append(c)
if len(freqs) == 0 or sum(freqs) != n:
raise ValueError
except:
freq = {}
get = freq.get
for ch in text:
freq[ch] = get(ch, 0) + 1
freqs = list(freq.values())
if len(freqs) == 1:
return {"encoded_length": n, "decoded": text}
freqs.sort()
total = 0
while len(freqs) > 1:
a = freqs.pop(0)
b = freqs.pop(0)
s = a + b
total += s
insort(freqs, s)
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
30μs slower
Code Size
50 vs 46 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | text = input.get("text", "") | 2 | text = input.get("text", "") |
| 3 | n = len(text) | 3 | n = len(text) |
| 4 | if n == 0: | 4 | if n == 0: |
| 5 | return {"encoded_length": 0, "decoded": ""} | 5 | return {"encoded_length": 0, "decoded": ""} |
| 6 | 6 | ||
| 7 | try: | 7 | counts = {} |
| 8 | from bisect import insort | 8 | get = counts.get |
| 9 | except: | 9 | for ch in text: |
| 10 | def insort(a, x): | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | lo, hi = 0, len(a) | 11 | |
| 12 | while lo < hi: | 12 | freqs = list(counts.values()) |
| 13 | mid = (lo + hi) >> 1 | 13 | m = len(freqs) |
| 14 | if a[mid] <= x: | 14 | |
| 15 | lo = mid + 1 | 15 | if m == 1: |
| 16 | else: | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | hi = mid | 17 | |
| 18 | a.insert(lo, x) | 18 | freqs.sort() |
| 19 | 19 | ||
| 20 | try: | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | counts = [0] * 256 | 21 | # use freqs as first queue and merged as second queue. |
| 22 | for ch in text: | 22 | merged = [0] * (m - 1) |
| 23 | counts[ord(ch)] += 1 | 23 | i = j = k = 0 |
| 24 | freqs = [] | 24 | total = 0 |
| 25 | for c in counts: | 25 | |
| 26 | if c: | 26 | while k < m - 1: |
| 27 | freqs.append(c) | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 28 | if len(freqs) == 0 or sum(freqs) != n: | 28 | a = freqs[i] |
| 29 | raise ValueError | 29 | i += 1 |
| 30 | except: | 30 | else: |
| 31 | freq = {} | 31 | a = merged[j] |
| 32 | get = freq.get | 32 | j += 1 |
| 33 | for ch in text: | 33 | |
| 34 | freq[ch] = get(ch, 0) + 1 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | freqs = list(freq.values()) | 35 | b = freqs[i] |
| 36 | 36 | i += 1 | |
| 37 | if len(freqs) == 1: | 37 | else: |
| 38 | return {"encoded_length": n, "decoded": text} | 38 | b = merged[j] |
| 39 | 39 | j += 1 | |
| 40 | freqs.sort() | 40 | |
| 41 | total = 0 | 41 | s = a + b |
| 42 | 42 | merged[k] = s | |
| 43 | while len(freqs) > 1: | 43 | total += s |
| 44 | a = freqs.pop(0) | 44 | k += 1 |
| 45 | b = freqs.pop(0) | 45 | |
| 46 | s = a + b | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | total += s | 47 | |
| 48 | insort(freqs, s) | 48 | |
| 49 | 49 | ||
| 50 | return {"encoded_length": total, "decoded": text} | 50 |
1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 try:8 from bisect import insort9 except:10 def insort(a, x):11 lo, hi = 0, len(a)12 while lo < hi:13 mid = (lo + hi) >> 114 if a[mid] <= x:15 lo = mid + 116 else:17 hi = mid18 a.insert(lo, x)1920 try:21 counts = [0] * 25622 for ch in text:23 counts[ord(ch)] += 124 freqs = []25 for c in counts:26 if c:27 freqs.append(c)28 if len(freqs) == 0 or sum(freqs) != n:29 raise ValueError30 except:31 freq = {}32 get = freq.get33 for ch in text:34 freq[ch] = get(ch, 0) + 135 freqs = list(freq.values())3637 if len(freqs) == 1:38 return {"encoded_length": n, "decoded": text}3940 freqs.sort()41 total = 04243 while len(freqs) > 1:44 a = freqs.pop(0)45 b = freqs.pop(0)46 s = a + b47 total += s48 insort(freqs, s)4950 return {"encoded_length": total, "decoded": text}1def solve(input):2 text = input.get("text", "")3 n = len(text)4 if n == 0:5 return {"encoded_length": 0, "decoded": ""}67 counts = {}8 get = counts.get9 for ch in text:10 counts[ch] = get(ch, 0) + 11112 freqs = list(counts.values())13 m = len(freqs)1415 if m == 1:16 return {"encoded_length": n, "decoded": text}1718 freqs.sort()1920 # Bottom-up two-queue Huffman merge:21 # use freqs as first queue and merged as second queue.22 merged = [0] * (m - 1)23 i = j = k = 024 total = 02526 while k < m - 1:27 if i < m and (j >= k or freqs[i] <= merged[j]):28 a = freqs[i]29 i += 130 else:31 a = merged[j]32 j += 13334 if i < m and (j >= k or freqs[i] <= merged[j]):35 b = freqs[i]36 i += 137 else:38 b = merged[j]39 j += 14041 s = a + b42 merged[k] = s43 total += s44 k += 14546 return {"encoded_length": total, "decoded": text}