Solution #29bbc470-d5f9-4aa4-88f5-8de8fa768be2

completed

Score

100% (5/5)

Runtime

95μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

Current100%Same as parent
268b5b53100%Same as parent
ffe6e932100%Same as parent
bb8a4da9100%Same as parent
0d32fca6100%Same as parent
195f1ac7100%Same as parent
96773990100%Same as parent
d7adae63100%Same as parent
8cb031c4100%Same as parent
0826d84e100%Same as parent
2da814bd100%Same as parent
e227904f100%Same as parent
69696638100%Same as parent
c128503d100%Same as parent
9e0f203b100%Same as parent
30447971100%Same as parent
62082b2d100%Same as parent
2a708353100%Same as parent
a0f415b0100%First in chain

Code

def solve(input):
    text = input.get("text", "")
    n = len(text)
    if not n:
        return {"encoded_length": 0, "decoded": ""}

    items = tuple(map(lambda kv: kv[1], __import__("collections").Counter(text).items()))
    if len(items) == 1:
        return {"encoded_length": n, "decoded": text}

    heapq = __import__("heapq")
    h = list(items)
    heapq.heapify(h)

    merged = []
    append = merged.append
    pop = heapq.heappop
    push = heapq.heappush

    def rec(k):
        return None if k == 0 else (
            lambda a=pop(h), b=pop(h), s=None: (
                push(h, a + b),
                append(a + b),
                rec(k - 1)
            )
        )()

    rec(len(h) - 1)
    return {"encoded_length": sum(merged), "decoded": text}

Compare with Champion

Score Difference

Tied

Runtime Advantage

71μs slower

Code Size

30 vs 46 lines

#Your Solution#Champion
1def solve(input):1def solve(input):
2 text = input.get("text", "")2 text = input.get("text", "")
3 n = len(text)3 n = len(text)
4 if not n:4 if n == 0:
5 return {"encoded_length": 0, "decoded": ""}5 return {"encoded_length": 0, "decoded": ""}
66
7 items = tuple(map(lambda kv: kv[1], __import__("collections").Counter(text).items()))7 counts = {}
8 if len(items) == 1:8 get = counts.get
9 return {"encoded_length": n, "decoded": text}9 for ch in text:
1010 counts[ch] = get(ch, 0) + 1
11 heapq = __import__("heapq")11
12 h = list(items)12 freqs = list(counts.values())
13 heapq.heapify(h)13 m = len(freqs)
1414
15 merged = []15 if m == 1:
16 append = merged.append16 return {"encoded_length": n, "decoded": text}
17 pop = heapq.heappop17
18 push = heapq.heappush18 freqs.sort()
1919
20 def rec(k):20 # Bottom-up two-queue Huffman merge:
21 return None if k == 0 else (21 # use freqs as first queue and merged as second queue.
22 lambda a=pop(h), b=pop(h), s=None: (22 merged = [0] * (m - 1)
23 push(h, a + b),23 i = j = k = 0
24 append(a + b),24 total = 0
25 rec(k - 1)25
26 )26 while k < m - 1:
27 )()27 if i < m and (j >= k or freqs[i] <= merged[j]):
2828 a = freqs[i]
29 rec(len(h) - 1)29 i += 1
30 return {"encoded_length": sum(merged), "decoded": text}30 else:
3131 a = merged[j]
3232 j += 1
3333
3434 if i < m and (j >= k or freqs[i] <= merged[j]):
3535 b = freqs[i]
3636 i += 1
3737 else:
3838 b = merged[j]
3939 j += 1
4040
4141 s = a + b
4242 merged[k] = s
4343 total += s
4444 k += 1
4545
4646 return {"encoded_length": total, "decoded": text}
Your Solution
100% (5/5)95μs
1def solve(input):
2 text = input.get("text", "")
3 n = len(text)
4 if not n:
5 return {"encoded_length": 0, "decoded": ""}
6
7 items = tuple(map(lambda kv: kv[1], __import__("collections").Counter(text).items()))
8 if len(items) == 1:
9 return {"encoded_length": n, "decoded": text}
10
11 heapq = __import__("heapq")
12 h = list(items)
13 heapq.heapify(h)
14
15 merged = []
16 append = merged.append
17 pop = heapq.heappop
18 push = heapq.heappush
19
20 def rec(k):
21 return None if k == 0 else (
22 lambda a=pop(h), b=pop(h), s=None: (
23 push(h, a + b),
24 append(a + b),
25 rec(k - 1)
26 )
27 )()
28
29 rec(len(h) - 1)
30 return {"encoded_length": sum(merged), "decoded": text}
Champion
100% (5/5)24μs
1def solve(input):
2 text = input.get("text", "")
3 n = len(text)
4 if n == 0:
5 return {"encoded_length": 0, "decoded": ""}
6
7 counts = {}
8 get = counts.get
9 for ch in text:
10 counts[ch] = get(ch, 0) + 1
11
12 freqs = list(counts.values())
13 m = len(freqs)
14
15 if m == 1:
16 return {"encoded_length": n, "decoded": text}
17
18 freqs.sort()
19
20 # 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 = 0
24 total = 0
25
26 while k < m - 1:
27 if i < m and (j >= k or freqs[i] <= merged[j]):
28 a = freqs[i]
29 i += 1
30 else:
31 a = merged[j]
32 j += 1
33
34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 b = freqs[i]
36 i += 1
37 else:
38 b = merged[j]
39 j += 1
40
41 s = a + b
42 merged[k] = s
43 total += s
44 k += 1
45
46 return {"encoded_length": total, "decoded": text}