File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44passes out of the box. Replace the body of ``compute_histogram`` with your
55own faster implementation.
66"""
7+ from collections import Counter
8+ from pathlib import Path
79
810
911def 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)
You can’t perform that action at this time.
0 commit comments