Skip to content

Commit ffd1889

Browse files
author
Sophia McKeever
committed
My solution
1 parent 9a09439 commit ffd1889

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

rounds/1_histogram/solution.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,31 @@
44
passes out of the box. Replace the body of ``compute_histogram`` with your
55
own faster implementation.
66
"""
7+
from collections import Counter
8+
from pathlib import Path
79

810

911
def compute_histogram(path: str) -> dict[bytes, int]:
1012
"""Frequency of every 2-byte bigram in the file at ``path``."""
1113
# TODO: remove this delegation and write your own implementation here.
12-
from .baseline import compute_histogram as _baseline
14+
with open(path, "rb") as file:
15+
data = file.read()
1316

14-
return _baseline(path)
17+
counts = [[0] * 256 for _ in range(256)]
18+
for index in range(len(data) - 1):
19+
counts[data[index]][data[index + 1]] += 1
20+
21+
result = {}
22+
x = y = 0
23+
while x < 256:
24+
result[bytes([x, y])] = counts[x][y]
25+
if y == 255:
26+
x += 1
27+
y = (y + 1) % 256
28+
29+
return result
30+
31+
# DATA_DIR = Path(__file__).parent / "data"
32+
# FIXTURE_PATH = DATA_DIR / "fixture_payload.bin"
33+
# PAYLOAD_PATH = DATA_DIR / "payload.bin"
34+
# result = compute_histogram(PAYLOAD_PATH)

0 commit comments

Comments
 (0)