题解 | #DNA序列#

DNA序列

https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a

import sys

def find_highGC_Ratio(str1,N): #先将每个字串的GC比例存入字典中
    result = {}
    for i in range(len(str1)-N):
        GC_sum = 0
        for j in str1[i:i+N]:
            if j in ['C','G']:
                GC_sum += 1
        GC_Ratio = GC_sum/N
        result[str1[i:i+N]] = GC_Ratio
    return result

def find(result):  #对字典进行遍历,将GC比例最高的字串提取出来
    max = 0
    max_value = ''
    for k,v in result.items():
        if v > max:
            max = v
            max_value = k
    return max_value

while True:
    try:
        str1 = input()
        N = int(input())
        if len(str1) == N:  #如果N和字符串长度相同则直接输出
            print(str1)
        else:
            result = find_highGC_Ratio(str1,N)
            print(find(result))
    except:
        break

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务