cs50 dna 当我调试我的代码时我得到了这个错误

这是我的代码,好的,我知道我的代码在算法上有 o^234234324 复杂性,但它适用于除 sequences/15.txt 和 sequences/16.txt 之外的所有序列


import sys

import csv

if len(sys.argv) != 3:

    print("useage: filenameofdata.cvs filenameofsequence.txt")

    sys.exit(1)

with open(sys.argv[1], "r") as datafile:

    readdata = list(csv.reader(datafile))

with open(sys.argv[2], "r") as sequencefile:

    readsequence = list(csv.reader(sequencefile))

strs = list(readdata[0][1:])

conlist = []

dnanum = 0

for move in (strs):

    sequence = list(readsequence[0][0])

    consecutively = 0

    l = len(move)

    cursor = [None] * 2

    temp = [None] * l

    x = 0

    counter = 0

    while counter == 0:

        if sequence == []:

            conlist.append(consecutively)

            break

        for oneletter in (sequence):

            if x < 2:

                cursor[x] = oneletter

            temp[x] = oneletter

            x += 1

            if x == l:

                asstring = ''.join(map(str, temp))

                if asstring == move:

                    dnanum += 1

                    move

                    temp = [None] * l

                    x = 0

                    continue

                else:

                    if consecutively < dnanum:

                        consecutively = dnanum

                    oneletter = sequence.remove(cursor[0])

                    temp = [None] * l

                    x = 0

                    dnanum = 0

                    break

# this print was for check if i got the right str consecutively

print(conlist)

conlist = ''.join(map(str, conlist))

for y in readdata:

    x = ''.join(map(str, y[1:]))

    if conlist == x:

        print(y[0])

        sys.exit(1)

print("No match")

当我尝试在 sequences/15.txt 和 sequences/16.txt 中调试它时,或者如果我尝试运行它们,我在调试时没有输出按摩错误

慕码人2483693
浏览 109回答 1
1回答

扬帆大鱼

那太复杂了。为了获得每个STR的最大连续STR数,我只写了6行代码:for i in range(1, len(data[0])):&nbsp; # loop through all STR&nbsp; &nbsp; count = 1&nbsp; &nbsp; string = data[0][i]&nbsp; # assign each STR to a string&nbsp; &nbsp; while string * count in dna:&nbsp; # if find 1 string, then try to find string*2, and so on&nbsp; &nbsp; &nbsp; &nbsp; count += 1&nbsp; &nbsp; counts.append(str(count - 1))&nbsp; # should be decreased by 1 as initialized to 1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python