Skip to content

Commit a18f83f

Browse files
Kiri11Kiri11
authored andcommitted
perf
1 parent 72790a0 commit a18f83f

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

rounds/1_histogram/solution.py

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

3-
**Edit this file.** It currently delegates to ``baseline.py`` so everything
4-
passes out of the box. Replace the body of ``compute_histogram`` with your
5-
own faster implementation.
6-
"""
3+
4+
import numpy as np
75

86

97
def compute_histogram(path: str) -> dict[bytes, int]:
10-
"""Frequency of every 2-byte bigram in the file at ``path``."""
11-
# TODO: remove this delegation and write your own implementation here.
12-
from .baseline import compute_histogram as _baseline
8+
data = np.fromfile(path, dtype=np.uint8)
9+
if len(data) < 2:
10+
return {}
1311

14-
return _baseline(path)
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)}

0 commit comments

Comments
 (0)