Skip to content

Commit f637d4c

Browse files
Kiri11Kiri11
authored andcommitted
attempt 2
1 parent a18f83f commit f637d4c

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

‎rounds/1_histogram/solution.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Round 1 solution — byte-pair histogram."""
22

3-
43
import numpy as np
54

65

@@ -9,8 +8,14 @@ def compute_histogram(path: str) -> dict[bytes, int]:
98
if len(data) < 2:
109
return {}
1110

12-
bigrams_16 = (data[:-1].astype(np.uint16) << 8) | data[1:]
13-
14-
values, counts = np.unique(bigrams_16, return_counts=True)
15-
16-
return {int(v).to_bytes(2, 'big'): int(c) for v, c in zip(values, counts)}
11+
bigrams = (data[:-1].astype(np.uint16) << 8) | data[1:]
12+
13+
counts = np.bincount(bigrams, minlength=65536)
14+
15+
valid_indices = np.nonzero(counts)[0]
16+
valid_counts = counts[valid_indices]
17+
18+
return {
19+
int(idx).to_bytes(2, 'big'): int(count)
20+
for idx, count in zip(valid_indices, valid_counts)
21+
}

0 commit comments

Comments
 (0)