Skip to content

Commit 76fb0a1

Browse files
TreyFitzgeraldTreyFitzgerald
authored andcommitted
Used Numpy
1 parent f55950f commit 76fb0a1

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

rounds/1_histogram/solution.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,25 @@
66
"""
77

88

9+
import numpy as np
10+
911
def compute_histogram(path: str) -> dict[bytes, int]:
1012
"""Frequency of every 2-byte bigram in the file at ``path``."""
11-
# more TODO: remove this delegation and write your own implementation here.
12-
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
1330

14-
return _baseline(path)

0 commit comments

Comments
 (0)