I am a beginner in cs50 and I have a problem with the DNA problem that gives the wrong result for most of the queries. I wonder if anyone could point where is the mistake. ie. python dna.py databases/small.csv sequences/1.txt supposed to give Bob, but I receive Alice as the result.

import csv
import sys

def main():

# to check input
if len(sys.argv) != 3:
    print('Usage: python dna.py data.csv sequence.txt')

# to read file
data = csv.DictReader(open(sys.argv[1]))
sequence = open(sys.argv[2]).read()
# print(sequence)

# to get counts for each sequence
counts = {}
for subseq in data.fieldnames[1:]:
    counts[subseq] = longest_match(sequence, subseq)
# print(counts)
# print(data.fieldnames[0:])

# to check for matching profile
for row in data:
    if (counts[subseq] == int(row[subseq]) for subseq in counts):
        print('No match')

# check match
def longest_match(sequence, subseq):
best = 0
length = len(subseq)
for i in range(len(sequence)):
    count = 0
    while True:
        start = i + count * length
        end = start + length
        # print('this is start', start)
        # print('this is end', end)
        if sequence[start:end] == subseq:
            count = count + 1
    best = max(best, count)
# print(count)

return best

1 Answer 1


Problem was caused by the check for matching profile section, needed to add an 'all' function to check all(counts[subseq]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .