Skip to content

Commit 20b763d

Browse files
committed
new impl
1 parent 2d136e2 commit 20b763d

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

rounds/1_histogram/solution.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
passes out of the box. Replace the body of ``compute_histogram`` with your
55
own faster implementation.
66
"""
7+
from collections import defaultdict
78

89

910
def compute_histogram(path: str) -> dict[bytes, int]:
1011
"""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
12+
with open(path, "rb") as f:
13+
data = f.read()
1314

14-
return _baseline(path)
15+
bytes_mat = [[0] * 256 for _ in range(256)]
16+
17+
for i in range(len(data) - 1):
18+
bytes_mat[data[i]][data[i + 1]] += 1
19+
20+
counts = {bytes([i, j]): bytes_mat[i][j] for i in range(256) for j in range(256)}
21+
22+
return counts

0 commit comments

Comments
 (0)