We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2d136e2 commit 20b763dCopy full SHA for 20b763d
1 file changed
rounds/1_histogram/solution.py
@@ -4,11 +4,19 @@
4
passes out of the box. Replace the body of ``compute_histogram`` with your
5
own faster implementation.
6
"""
7
+from collections import defaultdict
8
9
10
def compute_histogram(path: str) -> dict[bytes, int]:
11
"""Frequency of every 2-byte bigram in the file at ``path``."""
- # TODO: remove this delegation and write your own implementation here.
12
- from .baseline import compute_histogram as _baseline
+ with open(path, "rb") as f:
13
+ data = f.read()
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