Skip to content

Commit 1355f54

Browse files
committed
Used chatgpt AI for optimization 3
1 parent d03579d commit 1355f54

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

rounds/1_histogram/solution.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,29 @@
55
own faster implementation.
66
"""
77

8+
from array import array
9+
10+
811
def compute_histogram(path: str) -> dict[bytes, int]:
912
with open(path, "rb") as f:
1013
data = f.read()
1114

12-
counts = {}
15+
n = len(data)
16+
if n < 2:
17+
return {}
18+
19+
# 65,536 possible 2-byte combinations
20+
counts = array("I", [0]) * 65536
21+
22+
prev = data[0]
1323

14-
for a, b in zip(data, data[1:]):
15-
k = (a << 8) | b
16-
counts[k] = counts.get(k, 0) + 1
24+
for i in range(1, n):
25+
curr = data[i]
26+
counts[(prev << 8) | curr] += 1
27+
prev = curr
1728

1829
return {
19-
k.to_bytes(2, "big"): v
20-
for k, v in counts.items()
30+
i.to_bytes(2, "big"): count
31+
for i, count in enumerate(counts)
32+
if count
2133
}

0 commit comments

Comments
 (0)