Skip to content

Commit 8e2e902

Browse files
committed
update solution to use numpy
1 parent 4a6ab74 commit 8e2e902

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

rounds/1_histogram/solution.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
own faster implementation.
66
"""
77

8+
import numpy as np
9+
810

911
def compute_histogram(path: str) -> dict[bytes, int]:
1012
"""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
13+
# Step 1: read the whole file into memory as a single bytes object.
14+
with open(path, "rb") as f:
15+
data = f.read()
1316

14-
return _baseline(path)
17+
raw = np.frombuffer(data, dtype=np.uint8)
18+
bigrams = raw[:-1].astype(np.uint16) * 256 + raw[1:]
19+
unique, freq = np.unique(bigrams, return_counts=True)
20+
return {int(u).to_bytes(2, "big"): int(c) for u, c in zip(unique, freq)}

0 commit comments

Comments
 (0)