Skip to content

Commit a1659c6

Browse files
committed
Used chatgpt AI for optimization 1
1 parent 18ba26a commit a1659c6

1 file changed

Lines changed: 6 additions & 29 deletions

File tree

rounds/1_histogram/solution.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,11 @@
1313

1414
#return _baseline(path)
1515

16-
from array import array
16+
def histogram_dict(counts: list[int]) -> dict[bytes, int]:
17+
out = {}
1718

19+
for i, count in enumerate(counts):
20+
if count:
21+
out[i.to_bytes(2, "big")] = count
1822

19-
def compute_histogram(path: str) -> list[int]:
20-
"""
21-
Frequency table for every 2-byte bigram.
22-
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
23+
return out

0 commit comments

Comments
 (0)