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')
exit
# 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(row['name'])
return
else:
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
else:
break
best = max(best, count)
# print(count)
return best
main()