File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- """Your Round 1 solution — byte-pair histogram.
1+ """Round 1 solution — byte-pair histogram."""
22
3- **Edit this file.** It currently delegates to ``baseline.py`` so everything
4- passes out of the box. Replace the body of ``compute_histogram`` with your
5- own faster implementation.
6- """
3+
4+ import numpy as np
75
86
97def compute_histogram (path : str ) -> dict [bytes , int ]:
10- """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
8+ data = np . fromfile ( path , dtype = np . uint8 )
9+ if len ( data ) < 2 :
10+ return {}
1311
14- return _baseline (path )
12+ bigrams_16 = (data [:- 1 ].astype (np .uint16 ) << 8 ) | data [1 :]
13+
14+ values , counts = np .unique (bigrams_16 , return_counts = True )
15+
16+ return {int (v ).to_bytes (2 , 'big' ): int (c ) for v , c in zip (values , counts )}
You can’t perform that action at this time.
0 commit comments