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+ import numpy as np
78
89
910def compute_histogram (path : str ) -> dict [bytes , int ]:
@@ -15,11 +16,16 @@ def compute_histogram(path: str) -> dict[bytes, int]:
1516 # Step 2: slide a 2-byte window across the buffer. For ``b"ABCD"`` the
1617 # iterations produce ``b"AB"``, ``b"BC"``, then ``b"CD"``. For each window,
1718 # bump the matching bucket in a ``dict`` keyed by the bigram itself.
18- counts : dict [bytes , int ] = {}
19+ counts = [[0 ] * 256 for _ in range (256 )]
20+
1921 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
22+ a , b = data [i ], data [i + 1 ]
23+ counts [a ][b ] += 1
24+
25+ result = {}
26+ for i , row in enumerate (counts ):
27+ for j , count in enumerate (row ):
28+ if count > 0 :
29+ bigram = bytes ([i , j ])
30+ result [bigram ] = count
31+ return result
You can’t perform that action at this time.
0 commit comments