We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4a6ab74 commit 8e2e902Copy full SHA for 8e2e902
1 file changed
rounds/1_histogram/solution.py
@@ -5,10 +5,16 @@
5
own faster implementation.
6
"""
7
8
+import numpy as np
9
+
10
11
def compute_histogram(path: str) -> dict[bytes, int]:
12
"""Frequency of every 2-byte bigram in the file at ``path``."""
- # TODO: remove this delegation and write your own implementation here.
- 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()
16
- 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