Skip to content

Commit bf2bf4b

Browse files
committed
First change
1 parent 7b8a9e8 commit bf2bf4b

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ __pycache__/
1414

1515
# CodSpeed
1616
.codspeed/
17+
18+
# Mine
19+
codes

rounds/1_histogram/solution.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@
88

99
def compute_histogram(path: str) -> dict[bytes, int]:
1010
"""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
11+
# Step 1: read the whole file into memory as a single bytes object.
12+
with open(path, "rb") as f:
13+
data = f.read()
1314

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

0 commit comments

Comments
 (0)