Skip to content

Commit 3704c97

Browse files
Kiri11Kiri11
authored andcommitted
numpy
1 parent 72790a0 commit 3704c97

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

‎rounds/1_histogram/solution.py‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
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-
"""
73

4+
import numpy as np
85

96
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
7+
data = np.fromfile(path, dtype=np.uint8)
8+
if len(data) < 2:
9+
return {}
1310

14-
return _baseline(path)
11+
# View as 16-bit integers to represent the bigrams
12+
# We shift the first byte and OR it with the second
13+
bigrams_16 = (data[:-1].astype(np.uint16) << 8) | data[1:]
14+
15+
values, counts = np.unique(bigrams_16, return_counts=True)
16+
17+
# Convert back to the required dict[bytes, int] format
18+
return {int(v).to_bytes(2, 'big'): int(c) for v, c in zip(values, counts)}

0 commit comments

Comments
 (0)