Skip to content

Commit 8749555

Browse files
committed
Changed code
1 parent 8cbb2cb commit 8749555

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

rounds/3_dna/solution.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,37 @@
55
own faster implementation.
66
"""
77

8-
from .baseline import find_matches as _baseline
8+
from __future__ import annotations
99

1010

1111
def find_matches(fasta_path: str, pattern: bytes) -> list[tuple[str, list[int]]]:
1212
"""Find every FASTA record whose sequence contains ``pattern``.
1313
1414
Returns ``[(record_id, [positions...]), ...]`` in file order.
1515
"""
16-
# TODO: remove this delegation and write your own implementation here.
17-
return _baseline(fasta_path, pattern)
16+
with open(fasta_path, "rb") as f:
17+
text = f.read()
18+
19+
matches: list[tuple[str, list[int]]] = []
20+
21+
for record in text.split(b">"):
22+
if not record.strip():
23+
continue
24+
25+
lines = record.split(b"\n")
26+
record_id = lines[0].strip().decode("ascii")
27+
sequence = b"".join(lines[1:]).replace(b" ", b"")
28+
29+
positions: list[int] = []
30+
start = 0
31+
while True:
32+
pos = sequence.find(pattern, start)
33+
if pos == -1:
34+
break
35+
positions.append(pos)
36+
start = pos + 1
37+
38+
if positions:
39+
matches.append((record_id, positions))
40+
41+
return matches

0 commit comments

Comments
 (0)