Skip to content

Commit f6c85e2

Browse files
committed
Add solution 3
1 parent 3fbf155 commit f6c85e2

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

rounds/3_dna/solution.py

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

8-
from .baseline import find_matches as _baseline
8+
from concurrent.futures import ThreadPoolExecutor
9+
10+
11+
def _find_matches(pattern_str: str, record: str) -> tuple[str, list[int]]:
12+
lines = record.split("\n")
13+
record_id = lines[0].strip()
14+
sequence = "".join(lines[1:]).replace(" ", "")
15+
16+
positions: list[int] = []
17+
start = 0
18+
while True:
19+
pos = sequence.find(pattern_str, start)
20+
if pos == -1:
21+
break
22+
positions.append(pos)
23+
start = pos + 1
24+
25+
return (record_id, positions)
926

1027

1128
def find_matches(fasta_path: str, pattern: bytes) -> list[tuple[str, list[int]]]:
1229
"""Find every FASTA record whose sequence contains ``pattern``.
1330
1431
Returns ``[(record_id, [positions...]), ...]`` in file order.
1532
"""
16-
# TODO: remove this delegation and write your own implementation here.
17-
return _baseline(fasta_path, pattern)
33+
pattern_str = pattern.decode("ascii")
34+
matches = []
35+
with open(fasta_path, "r") as f:
36+
text = f.read()
37+
38+
with ThreadPoolExecutor(16) as ex:
39+
futures = [
40+
ex.submit(_find_matches, pattern_str, record)
41+
for record in text.split(">")
42+
if record.strip()
43+
]
44+
matches = [res for future in futures if (res := future.result())[1]]
45+
return matches

0 commit comments

Comments
 (0)