Skip to content

Commit 48e4235

Browse files
committed
Attempt 3
1 parent f637d4c commit 48e4235

9 files changed

Lines changed: 75 additions & 9 deletions

File tree

.idea/.gitignore

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/pyProjectModel.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/pyconus-2026-tutorial.iml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rounds/1_histogram/solution.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
"""Round 1 solution — byte-pair histogram."""
2-
1+
import os
2+
import mmap
33
import numpy as np
44

5+
_BIGRAM_BYTES = tuple(i.to_bytes(2, "big") for i in range(65536))
56

67
def compute_histogram(path: str) -> dict[bytes, int]:
7-
data = np.fromfile(path, dtype=np.uint8)
8-
if len(data) < 2:
8+
if os.path.getsize(path) < 2:
99
return {}
1010

11-
bigrams = (data[:-1].astype(np.uint16) << 8) | data[1:]
11+
with open(path, "rb") as f:
12+
with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
13+
byte_values = np.frombuffer(mm, dtype=np.uint8)
1214

13-
counts = np.bincount(bigrams, minlength=65536)
15+
bigrams = byte_values[:-1].astype(np.uint16)
16+
bigrams <<= 8
17+
bigrams |= byte_values[1:]
1418

19+
del byte_values
20+
21+
counts = np.bincount(bigrams, minlength=65536)
1522
valid_indices = np.nonzero(counts)[0]
16-
valid_counts = counts[valid_indices]
1723

1824
return {
19-
int(idx).to_bytes(2, 'big'): int(count)
20-
for idx, count in zip(valid_indices, valid_counts)
25+
_BIGRAM_BYTES[idx]: counts[idx].item()
26+
for idx in valid_indices
2127
}

rounds/3_dna/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)