Solution #15ec7bd1-f974-4ad2-b7cb-c562c061bacf
completedScore
100% (5/5)
Runtime
49μs
Delta
No change vs parent
Tied for best
Same as parent
Score
100% (5/5)
Runtime
49μ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:
b = text.encode("latin1")
counts = [0] * 256
for x in b:
counts[x] += 1
freqs = []
for c in counts:
if c:
freqs.append(c)
except:
counts = {}
get = counts.get
for ch in text:
counts[ch] = get(ch, 0) + 1
freqs = list(counts.values())
m = len(freqs)
if m == 1:
return {"encoded_length": n, "decoded": text}
freqs.sort()
if m == 2:
return {"encoded_length": freqs[0] + freqs[1], "decoded": text}
merged = [0] * (m - 1)
i = j = k = 0
total = 0
while k < m - 1:
fi = freqs
mg = merged
if j >= k or (i < m and fi[i] <= mg[j]):
a = fi[i]
i += 1
else:
a = mg[j]
j += 1
if j >= k or (i < m and fi[i] <= mg[j]):
b2 = fi[i]
i += 1
else:
b2 = mg[j]
j += 1
s = a + b2
mg[k] = s
total += s
k += 1
return {"encoded_length": total, "decoded": text}Score Difference
Tied
Runtime Advantage
25μs slower
Code Size
60 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 | b = text.encode("latin1") | 8 | get = counts.get |
| 9 | counts = [0] * 256 | 9 | for ch in text: |
| 10 | for x in b: | 10 | counts[ch] = get(ch, 0) + 1 |
| 11 | counts[x] += 1 | 11 | |
| 12 | 12 | freqs = list(counts.values()) | |
| 13 | freqs = [] | 13 | m = len(freqs) |
| 14 | for c in counts: | 14 | |
| 15 | if c: | 15 | if m == 1: |
| 16 | freqs.append(c) | 16 | return {"encoded_length": n, "decoded": text} |
| 17 | except: | 17 | |
| 18 | counts = {} | 18 | freqs.sort() |
| 19 | get = counts.get | 19 | |
| 20 | for ch in text: | 20 | # Bottom-up two-queue Huffman merge: |
| 21 | counts[ch] = get(ch, 0) + 1 | 21 | # use freqs as first queue and merged as second queue. |
| 22 | freqs = list(counts.values()) | 22 | merged = [0] * (m - 1) |
| 23 | 23 | i = j = k = 0 | |
| 24 | m = len(freqs) | 24 | total = 0 |
| 25 | if m == 1: | 25 | |
| 26 | return {"encoded_length": n, "decoded": text} | 26 | while k < m - 1: |
| 27 | 27 | if i < m and (j >= k or freqs[i] <= merged[j]): | |
| 28 | freqs.sort() | 28 | a = freqs[i] |
| 29 | 29 | i += 1 | |
| 30 | if m == 2: | 30 | else: |
| 31 | return {"encoded_length": freqs[0] + freqs[1], "decoded": text} | 31 | a = merged[j] |
| 32 | 32 | j += 1 | |
| 33 | merged = [0] * (m - 1) | 33 | |
| 34 | i = j = k = 0 | 34 | if i < m and (j >= k or freqs[i] <= merged[j]): |
| 35 | total = 0 | 35 | b = freqs[i] |
| 36 | 36 | i += 1 | |
| 37 | while k < m - 1: | 37 | else: |
| 38 | fi = freqs | 38 | b = merged[j] |
| 39 | mg = merged | 39 | j += 1 |
| 40 | 40 | ||
| 41 | if j >= k or (i < m and fi[i] <= mg[j]): | 41 | s = a + b |
| 42 | a = fi[i] | 42 | merged[k] = s |
| 43 | i += 1 | 43 | total += s |
| 44 | else: | 44 | k += 1 |
| 45 | a = mg[j] | 45 | |
| 46 | j += 1 | 46 | return {"encoded_length": total, "decoded": text} |
| 47 | 47 | ||
| 48 | if j >= k or (i < m and fi[i] <= mg[j]): | 48 | |
| 49 | b2 = fi[i] | 49 | |
| 50 | i += 1 | 50 | |
| 51 | else: | 51 | |
| 52 | b2 = mg[j] | 52 | |
| 53 | j += 1 | 53 | |
| 54 | 54 | ||
| 55 | s = a + b2 | 55 | |
| 56 | mg[k] = s | 56 | |
| 57 | total += s | 57 | |
| 58 | k += 1 | 58 | |
| 59 | 59 | ||
| 60 | return {"encoded_length": total, "decoded": text} | 60 |
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 b = text.encode("latin1")9 counts = [0] * 25610 for x in b:11 counts[x] += 11213 freqs = []14 for c in counts:15 if c:16 freqs.append(c)17 except:18 counts = {}19 get = counts.get20 for ch in text:21 counts[ch] = get(ch, 0) + 122 freqs = list(counts.values())2324 m = len(freqs)25 if m == 1:26 return {"encoded_length": n, "decoded": text}2728 freqs.sort()2930 if m == 2:31 return {"encoded_length": freqs[0] + freqs[1], "decoded": text}3233 merged = [0] * (m - 1)34 i = j = k = 035 total = 03637 while k < m - 1:38 fi = freqs39 mg = merged4041 if j >= k or (i < m and fi[i] <= mg[j]):42 a = fi[i]43 i += 144 else:45 a = mg[j]46 j += 14748 if j >= k or (i < m and fi[i] <= mg[j]):49 b2 = fi[i]50 i += 151 else:52 b2 = mg[j]53 j += 15455 s = a + b256 mg[k] = s57 total += s58 k += 15960 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}