-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_repeated_dna_sequences.py
More file actions
57 lines (42 loc) · 1.39 KB
/
find_repeated_dna_sequences.py
File metadata and controls
57 lines (42 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from time import perf_counter_ns
def map_nucleotides():
"""Map nucleotide characters to numbers."""
return {"A": 0, "C": 1, "G": 2, "T": 3}
def compute_base(length=10):
"""Precompute the base for rolling hash (4^(length-1))."""
return 4 ** (length - 1)
def find_repeated_sequences(s, nucleotide_map, base, length=10):
"""Return all 10-letter-long sequences that occur more than once."""
n = len(s)
if n < length:
return []
repeated = set()
# Compute initial hash
h = 0
for i in range(length):
h = h * 4 + nucleotide_map[s[i]]
seen = {h}
# Rolling hash
for i in range(1, n - length + 1):
h = (h - nucleotide_map[s[i - 1]] * base) * 4 + nucleotide_map[
s[i + length - 1]
]
if h in seen:
repeated.add(s[i : i + length])
else:
seen.add(h)
return list(repeated)
def main():
s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
nucleotide_map = map_nucleotides()
base = compute_base(10)
start_time = perf_counter_ns()
for _ in range(1000):
ans = find_repeated_sequences(s, nucleotide_map, base, 10)
print(ans)
end_time = perf_counter_ns()
total_time = end_time - start_time
print("Total time for 1000 iterations:", total_time, "ns")
print("Average time per iteration:", total_time / 1000, "ns")
if __name__ == "__main__":
main()