Skip to content

Commit 1a21fe6

Browse files
committed
Fix file
1 parent 9d01718 commit 1a21fe6

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

rounds/1_histogram/baseline.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44
tokens) in a binary payload.
55
"""
66

7-
from collections import Counter
8-
from itertools import pairwise
9-
107

118
def compute_histogram(path: str) -> dict[bytes, int]:
129
"""Frequency of every 2-byte bigram in the file at ``path``."""
10+
# Step 1: read the whole file into memory as a single bytes object.
1311
with open(path, "rb") as f:
1412
data = f.read()
1513

16-
return Counter(bytes(bigram) for bigram in pairwise(data))
14+
# Step 2: slide a 2-byte window across the buffer. For ``b"ABCD"`` the
15+
# iterations produce ``b"AB"``, ``b"BC"``, then ``b"CD"``. For each window,
16+
# bump the matching bucket in a ``dict`` keyed by the bigram itself.
17+
counts: dict[bytes, int] = {}
18+
for i in range(len(data) - 1):
19+
bigram = data[i : i + 2]
20+
if bigram in counts:
21+
counts[bigram] += 1
22+
else:
23+
counts[bigram] = 1
24+
return counts

rounds/1_histogram/solution.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
own faster implementation.
66
"""
77

8+
from collections import Counter
9+
from itertools import pairwise
10+
from pathlib import Path
11+
812

913
def compute_histogram(path: str) -> dict[bytes, int]:
1014
"""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
13-
14-
return _baseline(path)
15+
return Counter(bytes(bigram) for bigram in pairwise(Path(path).read_bytes()))

0 commit comments

Comments
 (0)