Solution #f68f5746-1281-447a-910e-83fc1d850202

completed

Score

100% (5/5)

Runtime

85μs

Delta

No change vs parent

Tied for best

Same as parent

Solution Lineage

Current100%Same as parent
40369b8a100%Same as parent
f9d7018b100%Same as parent
5a4f6922100%Same as parent
15ec7bd1100%Same as parent
76aeaf5e100%Improved from parent
4b16e42760%Regression from parent
fd0fb3a5100%Same as parent
4911088c100%Same as parent
68ce1629100%Same as parent
2d8382ce100%Same as parent
62c233ed100%Same as parent
82a9952b100%Same as parent
b513de2d100%Same as parent
8607bb8f100%Same as parent
96012705100%Same as parent
21d15e87100%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 n == 0:
        return {"encoded_length": 0, "decoded": ""}

    # Precomputed popcount table for wildcard direct-indexing requirement
    POP8 = (
        0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
        1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
        1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
        1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
        3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
        1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
        3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
        3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
        3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
        4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
    )

    # Radically different frequency counting:
    # bitset per byte value using Python big ints, then direct-index popcount table.
    try:
        b = text.encode("latin1")
        masks = [0] * 256
        bit = 1
        for c in b:
            masks[c] |= bit
            bit <<= 1

        freqs = []
        append = freqs.append
        uniq = 0
        for m in masks:
            if m:
                uniq += 1
                cnt = 0
                while m:
                    cnt += POP8[m & 255]
                    m >>= 8
                append(cnt)
    except:
        counts = {}
        get = counts.get
        for ch in text:
            counts[ch] = get(ch, 0) + 1
        freqs = list(counts.values())
        uniq = len(freqs)

    if uniq <= 2:
        return {"encoded_length": n, "decoded": text}

    # Two-queue Huffman after sorting frequencies, avoiding heap operations.
    freqs.sort()
    m = len(freqs)
    q2 = [0] * (m - 1)
    i = j = k = 0
    total = 0

    def take():
        nonlocal i, j
        if i < m and (j >= k or freqs[i] <= q2[j]):
            v = freqs[i]
            i += 1
            return v
        v = q2[j]
        j += 1
        return v

    while k < m - 1:
        s = take() + take()
        total += s
        q2[k] = s
        k += 1

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

Compare with Champion

Score Difference

Tied

Runtime Advantage

61μs slower

Code Size

82 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 n == 0:4 if n == 0:
5 return {"encoded_length": 0, "decoded": ""}5 return {"encoded_length": 0, "decoded": ""}
66
7 # Precomputed popcount table for wildcard direct-indexing requirement7 counts = {}
8 POP8 = (8 get = counts.get
9 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,9 for ch in text:
10 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,10 counts[ch] = get(ch, 0) + 1
11 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,11
12 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,12 freqs = list(counts.values())
13 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,13 m = len(freqs)
14 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,14
15 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,15 if m == 1:
16 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,16 return {"encoded_length": n, "decoded": text}
17 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,17
18 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,18 freqs.sort()
19 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,19
20 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,20 # Bottom-up two-queue Huffman merge:
21 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,21 # use freqs as first queue and merged as second queue.
22 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,22 merged = [0] * (m - 1)
23 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,23 i = j = k = 0
24 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,824 total = 0
25 )25
2626 while k < m - 1:
27 # Radically different frequency counting:27 if i < m and (j >= k or freqs[i] <= merged[j]):
28 # bitset per byte value using Python big ints, then direct-index popcount table.28 a = freqs[i]
29 try:29 i += 1
30 b = text.encode("latin1")30 else:
31 masks = [0] * 25631 a = merged[j]
32 bit = 132 j += 1
33 for c in b:33
34 masks[c] |= bit34 if i < m and (j >= k or freqs[i] <= merged[j]):
35 bit <<= 135 b = freqs[i]
3636 i += 1
37 freqs = []37 else:
38 append = freqs.append38 b = merged[j]
39 uniq = 039 j += 1
40 for m in masks:40
41 if m:41 s = a + b
42 uniq += 142 merged[k] = s
43 cnt = 043 total += s
44 while m:44 k += 1
45 cnt += POP8[m & 255]45
46 m >>= 846 return {"encoded_length": total, "decoded": text}
47 append(cnt)47
48 except:48
49 counts = {}49
50 get = counts.get50
51 for ch in text:51
52 counts[ch] = get(ch, 0) + 152
53 freqs = list(counts.values())53
54 uniq = len(freqs)54
5555
56 if uniq <= 2:56
57 return {"encoded_length": n, "decoded": text}57
5858
59 # Two-queue Huffman after sorting frequencies, avoiding heap operations.59
60 freqs.sort()60
61 m = len(freqs)61
62 q2 = [0] * (m - 1)62
63 i = j = k = 063
64 total = 064
6565
66 def take():66
67 nonlocal i, j67
68 if i < m and (j >= k or freqs[i] <= q2[j]):68
69 v = freqs[i]69
70 i += 170
71 return v71
72 v = q2[j]72
73 j += 173
74 return v74
7575
76 while k < m - 1:76
77 s = take() + take()77
78 total += s78
79 q2[k] = s79
80 k += 180
8181
82 return {"encoded_length": total, "decoded": text}82
Your Solution
100% (5/5)85μ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 # Precomputed popcount table for wildcard direct-indexing requirement
8 POP8 = (
9 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
10 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
11 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
12 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
13 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
14 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
15 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
16 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
17 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
18 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
19 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
20 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
21 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
22 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
23 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
24 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
25 )
26
27 # Radically different frequency counting:
28 # bitset per byte value using Python big ints, then direct-index popcount table.
29 try:
30 b = text.encode("latin1")
31 masks = [0] * 256
32 bit = 1
33 for c in b:
34 masks[c] |= bit
35 bit <<= 1
36
37 freqs = []
38 append = freqs.append
39 uniq = 0
40 for m in masks:
41 if m:
42 uniq += 1
43 cnt = 0
44 while m:
45 cnt += POP8[m & 255]
46 m >>= 8
47 append(cnt)
48 except:
49 counts = {}
50 get = counts.get
51 for ch in text:
52 counts[ch] = get(ch, 0) + 1
53 freqs = list(counts.values())
54 uniq = len(freqs)
55
56 if uniq <= 2:
57 return {"encoded_length": n, "decoded": text}
58
59 # Two-queue Huffman after sorting frequencies, avoiding heap operations.
60 freqs.sort()
61 m = len(freqs)
62 q2 = [0] * (m - 1)
63 i = j = k = 0
64 total = 0
65
66 def take():
67 nonlocal i, j
68 if i < m and (j >= k or freqs[i] <= q2[j]):
69 v = freqs[i]
70 i += 1
71 return v
72 v = q2[j]
73 j += 1
74 return v
75
76 while k < m - 1:
77 s = take() + take()
78 total += s
79 q2[k] = s
80 k += 1
81
82 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}