首页 > 解决方案 > CS50 DNA: STR counter only works most of the time

问题描述

Here is a mock up of the function. A lot of the samples have one or two STRs coming back as 1. Can someone help me understand what I am doing wrong?

dnaSamp = input("DNA: ")
strSeq = ["TATC"]###["AGATC", "TTTTTTCT", "AATG", "TCTAG", "GATA", "TATC", "GAAA", "TCTG"]
hiScore = [0] * len(strSeq)
for i in range(len(strSeq)):  # cycle throught the varios STRs
    for j in range(len(dnaSamp) - (len(strSeq)-1)):  # loop over dna sample  
        k = j + len(strSeq[i])  # variable to control the length of the STR sequence
    
        if dnaSamp[j : k] == strSeq[i]:
            counter = 0
        
            for l in range(len(dnaSamp)):        #if match look at next set
                if dnaSamp[j + (l  * len(strSeq[i])) : k + (l * len(strSeq[i]))] == strSeq[i]:
                    counter += 1
                    continue
                break
            if counter > hiScore[i]:
                hiScore[i] = counter    #save highest counter
        
    print(f"{strSeq[i]} = {hiScore[i]}" )

标签: pythoncs50

解决方案


推荐阅读