听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • 《Go语言实战》

  • 《Go Web编程》

  • 《算法精粹 经典计算机科学问题的Python实现》

    • 几个小问题

    • 搜索问题

      • DNA搜索
        • DNA的存储方法
        • 线性搜索
        • 二分搜索
        • 通用示例
      • 求解迷宫问题
    • 约束满足问题

    • 图问题

    • 遗传算法

    • k均值聚类

    • 简单的神经网络

    • 对抗搜索

    • 其他问题

  • 读书笔记
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 搜索问题
zfprotectors
2022-05-18
目录

DNA搜索

在生物学中,3个核苷酸(A,C,T,G)可以组合成一个密码子。生物信息学软件的一个经典任务就是在基因中找到某个特定的密码子。

# DNA的存储方法

核苷酸可以表示为4中状态的简单类型IntEnum

from enum import IntEnum
from typing import Tuple, List

Nucleotide: IntEnum = IntEnum('Nucleotide', ('A', 'T', 'C', 'G'))
Codon = Tuple[Nucleotide, Nucleotide, Nucleotide]
Gene = List[Codon]


gene_str: str = "ACGTGGCTCTCTAACGTACGTACGTACGGGGTTTATATATACCCTAGGACTCCCTTT"

def string_to_gene(s: str) -> Gene:
    gene: Gene = []
    for i in range(0, len(s), 3):
        if (i+2) >= len(s):
            return gene
        codon: Codon = (Nucleotide[s[i]], Nucleotide[s[i+1]], Nucleotide[s[i+2]])
        gene.append(codon)
    return gene

my_gene: Gene = string_to_gene(gene_str)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Python新增了IntEnum, IntEnum允许枚举的成员和整型作比较

# 线性搜索

基因需要执行的最基本的操作就是搜索指定的密码子,目标就是简单的查找该密码子是否存在于基因中。

线性搜索,顾名思义,就是按照从头到尾,按照原本的顺序进行搜索每个元素,一直查询到结果或者搜索的结尾。

最坏复杂度:O(n)

def line_contains(gene:Gene, key_codon:Codon)->bool:
    for codon in gene:
        if codon == key_codon:
            return True
    return False

1
2
3
4
5
6

提示

当然(list,tuple,range)均已实现了__contains__()方法,这样就能用in操作符在其中搜索某个指定的数据项

例如,执行print(acg in my_gene)语句即可在my_gene中搜索acg并打印出结果

# 二分搜索

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
1
2
3
4
5
6
7
8
9
10
11
12

# 通用示例

from __future__ import annotations
from typing import TypeVar, Iterable, Sequence, Generic, List, Callable, Set, Deque, Dict, Any, Optional
from typing import Protocol
from heapq import heappush, heappop

T = TypeVar('T')


def linear_contains(iterable: Iterable[T], key: T) -> bool:
    for item in iterable:
        if item == key:
            return True
    return False


C = TypeVar("C", bound="Comparable")


class Comparable(Protocol):
    def __eq__(self, other: Any) -> bool:
        pass

    def __lt__(self: C, other: C) -> bool:
        pass

    def __gt__(self: C, other: C) -> bool:
        return (not self < other) and self != other

    def __le__(self: C, other: C) -> bool:
        return self < other or self == other

    def __ge__(self: C, other: C) -> bool:
        return not self < other


def binary_contains(sequence: Sequence[C], key: C) -> bool:
    low: int = 0
    high: int = len(sequence) - 1
    while low <= high:
        mid: int = (low + high) // 2
        if sequence[mid] < key:
            low = mid + 1
        elif sequence[mid] > key:
            high = mid - 1
        else:
            return True
    return False


if __name__ == "__main__":
    print(linear_contains([1, 5, 15, 15, 15, 15, 20], 5))
    print(binary_contains(["a", "d", "e", "f", "z"], "f"))
    print(binary_contains(["john", "mark", "ronald", "sarah"], "sheila"))
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

提示

以下内容需要使用pip3 install typing_extensions安装typing_extensions模块,

而后续版本的标准库将会包含整个类型(PEP544)

因此Python的后续版本将不再需要导入该模块,并替换该模块导入部分: from typing import Protocol 替换为 from typing_extensions import Protocol

编辑 (opens new window)
上次更新: 2022/05/25, 16:53:28
汉诺塔
求解迷宫问题

← 汉诺塔 求解迷宫问题→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式