-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdna_search.py
31 lines (24 loc) · 844 Bytes
/
dna_search.py
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
from enum import IntEnum
from typing import Tuple, List
Nucleotide = IntEnum('Nucleotide', ('A', 'C', 'G', 'T'))
Codon = Tuple[Nucleotide, Nucleotide, Nucleotide]
Gene = List[Codon]
def string_to_gene(s: str) -> Gene:
gene = [(Nucleotide[s[i]], Nucleotide[s[i + 1]], Nucleotide[s[i + 2]])
for i in range(0, len(s), 3)
if i + 2 < len(s)]
return gene
def linear_contains(gene: Gene, key_codon: Codon) -> bool:
return any(codon == key_codon for codon in gene)
def binary_contains(gene: Gene, key_codon: Codon) -> bool:
low: int = 0
high: int = len(gene) - 1
while low <= high:
mid: int = (low + high) // 2
if gene[mid] < key_codon:
low = mid + 1
elif gene[mid] > key_codon:
high = mid - 1
else:
return True
return False