We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 18ba26a commit a1659c6Copy full SHA for a1659c6
1 file changed
rounds/1_histogram/solution.py
@@ -13,34 +13,11 @@
13
14
#return _baseline(path)
15
16
-from array import array
+def histogram_dict(counts: list[int]) -> dict[bytes, int]:
17
+ out = {}
18
19
+ for i, count in enumerate(counts):
20
+ if count:
21
+ out[i.to_bytes(2, "big")] = count
22
-def compute_histogram(path: str) -> list[int]:
- """
- Frequency table for every 2-byte bigram.
-
23
- Result index:
24
- index = (byte1 << 8) | byte2
25
26
- Example:
27
- b"AB" -> (65 << 8) | 66
28
29
- with open(path, "rb") as f:
30
- data = f.read()
31
32
- n = len(data)
33
- if n < 2:
34
- return [0] * 65536
35
36
- # Fixed-size contiguous integer array
37
- counts = array('I', [0]) * 65536
38
39
- prev = data[0]
40
41
- for i in range(1, n):
42
- curr = data[i]
43
- counts[(prev << 8) | curr] += 1
44
- prev = curr
45
46
- return counts
+ return out
0 commit comments