We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f55950f commit 76fb0a1Copy full SHA for 76fb0a1
1 file changed
rounds/1_histogram/solution.py
@@ -6,9 +6,25 @@
6
"""
7
8
9
+import numpy as np
10
+
11
def compute_histogram(path: str) -> dict[bytes, int]:
12
"""Frequency of every 2-byte bigram in the file at ``path``."""
- # more TODO: remove this delegation and write your own implementation here.
- from .baseline import compute_histogram as _baseline
13
+ data = np.fromfile(path, dtype=np.uint8)
14
15
+ if len(data) < 2:
16
+ return {}
17
18
+ bigrams = (data[:-1].astype(np.uint16) << 8) | data[1:]
19
+ counts = np.bincount(bigrams, minlength=65536)
20
21
+ result = {}
22
+ nonzero_indices = np.nonzero(counts)[0]
23
24
+ for idx in nonzero_indices:
25
+ byte1 = (idx >> 8) & 0xFF
26
+ byte2 = idx & 0xFF
27
+ result[bytes([byte1, byte2])] = int(counts[idx])
28
29
+ return result
30
- return _baseline(path)
0 commit comments