Solution #21d15e87-6c59-4118-a89e-e523c6e1802f

completed

Score

100% (5/5)

Runtime

58μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

Current100%Same as parent
9a277008100%Improved from parent
1b2fa39920%Regression from parent
98fd02a3100%Same as parent
ae4d0f99100%Same as parent
6126deee100%Same as parent
d720f0bf100%Same as parent
7e637902100%Same as parent
29bbc470100%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": ""}

    try:
        counts = [0] * 256
        for ch in text:
            counts[ord(ch)] += 1

        freqs = ()
        for c in counts:
            if c:
                freqs += (c,)
    except:
        d = {}
        for ch in text:
            d[ch] = d.get(ch, 0) + 1
        freqs = ()
        for c in d.values():
            freqs += (c,)

    m = len(freqs)
    if m == 1:
        return {"encoded_length": n, "decoded": text}

    freqs = sorted(freqs)
    q = [0] * (m - 1)
    i = j = qn = total = 0

    for _ in text[:0].join(" " * (m - 1)):
        if i < m and (j >= qn or freqs[i] <= q[j]):
            a = freqs[i]
            i += 1
        else:
            a = q[j]
            j += 1

        if i < m and (j >= qn or freqs[i] <= q[j]):
            b = freqs[i]
            i += 1
        else:
            b = q[j]
            j += 1

        s = a + b
        total += s
        q[qn] = s
        qn += 1

    return {"encoded_length": total, "decoded": text}

Compare with Champion

Score Difference

Tied

Runtime Advantage

34μs slower

Code Size

52 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 try:7 counts = {}
8 counts = [0] * 2568 get = counts.get
9 for ch in text:9 for ch in text:
10 counts[ord(ch)] += 110 counts[ch] = get(ch, 0) + 1
1111
12 freqs = ()12 freqs = list(counts.values())
13 for c in counts:13 m = len(freqs)
14 if c:14
15 freqs += (c,)15 if m == 1:
16 except:16 return {"encoded_length": n, "decoded": text}
17 d = {}17
18 for ch in text:18 freqs.sort()
19 d[ch] = d.get(ch, 0) + 119
20 freqs = ()20 # Bottom-up two-queue Huffman merge:
21 for c in d.values():21 # use freqs as first queue and merged as second queue.
22 freqs += (c,)22 merged = [0] * (m - 1)
2323 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:
2727 if i < m and (j >= k or freqs[i] <= merged[j]):
28 freqs = sorted(freqs)28 a = freqs[i]
29 q = [0] * (m - 1)29 i += 1
30 i = j = qn = total = 030 else:
3131 a = merged[j]
32 for _ in text[:0].join(" " * (m - 1)):32 j += 1
33 if i < m and (j >= qn or freqs[i] <= q[j]):33
34 a = freqs[i]34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 i += 135 b = freqs[i]
36 else:36 i += 1
37 a = q[j]37 else:
38 j += 138 b = merged[j]
3939 j += 1
40 if i < m and (j >= qn or freqs[i] <= q[j]):40
41 b = freqs[i]41 s = a + b
42 i += 142 merged[k] = s
43 else:43 total += s
44 b = q[j]44 k += 1
45 j += 145
4646 return {"encoded_length": total, "decoded": text}
47 s = a + b47
48 total += s48
49 q[qn] = s49
50 qn += 150
5151
52 return {"encoded_length": total, "decoded": text}52
Your Solution
100% (5/5)58μ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 try:
8 counts = [0] * 256
9 for ch in text:
10 counts[ord(ch)] += 1
11
12 freqs = ()
13 for c in counts:
14 if c:
15 freqs += (c,)
16 except:
17 d = {}
18 for ch in text:
19 d[ch] = d.get(ch, 0) + 1
20 freqs = ()
21 for c in d.values():
22 freqs += (c,)
23
24 m = len(freqs)
25 if m == 1:
26 return {"encoded_length": n, "decoded": text}
27
28 freqs = sorted(freqs)
29 q = [0] * (m - 1)
30 i = j = qn = total = 0
31
32 for _ in text[:0].join(" " * (m - 1)):
33 if i < m and (j >= qn or freqs[i] <= q[j]):
34 a = freqs[i]
35 i += 1
36 else:
37 a = q[j]
38 j += 1
39
40 if i < m and (j >= qn or freqs[i] <= q[j]):
41 b = freqs[i]
42 i += 1
43 else:
44 b = q[j]
45 j += 1
46
47 s = a + b
48 total += s
49 q[qn] = s
50 qn += 1
51
52 return {"encoded_length": total, "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}