my code works for all the other tests on check50 besides the one for txt18. I checked the Large.csv file and the return value of longest_match function (both provided by cs50) and they match up yet cs50 expects "No match".
any design notes and criticism of code is welcomed, I am trying to better my coding skills and style import csv import sys
def main():
# TODO: Check for command-line usage
if len(sys.argv) != 3:
sys.exit("Usage: python dna.py data.csv sequence.txt")
# TODO: Read database file into a variable
data = []
with open (sys.argv[1], "r") as file:
reader = csv.DictReader(file)
for row in reader:
tmp = {}
row["name"] = row["name"]
row["AGATC"] = int(row["AGATC"])
row["AATG"] = int(row["AATG"])
row["TATC"] = int(row["TATC"])
tmp = row
data.append(tmp)
# TODO: Read DNA sequence file into a variable
file = open(sys.argv[2], "r")
dna = file.readline()
file.close()
# TODO: Find longest match of each STR in DNA sequence
frequency_AGATC = longest_match(dna, "AGATC")
frequency_AATG = longest_match(dna, "AATG")
frequency_TATC = longest_match(dna, "TATC")
# TODO: Check database for matching profiles
for i in range(len(data)):
if data[i]["AGATC"] == frequency_AGATC and data[i]["AATG"] == frequency_AATG and data[i]["TATC"] == frequency_TATC:
print(data[i]["name"])
return 0
print("No match")
return 1
def longest_match(sequence, subsequence): """Returns length of longest run of subsequence in sequence."""
# Initialize variables
longest_run = 0
subsequence_length = len(subsequence)
sequence_length = len(sequence)
# Check each character in sequence for most consecutive runs of subsequence
for i in range(sequence_length):
# Initialize count of consecutive runs
count = 0
# Check for a subsequence match in a "substring" (a subset of characters) within sequence
# If a match, move substring to next potential match in sequence
# Continue moving substring and checking for matches until out of consecutive matches
while True:
# Adjust substring start and end
start = i + count * subsequence_length
end = start + subsequence_length
# If there is a match in the substring
if sequence[start:end] == subsequence:
count += 1
# If there is no match in the substring
else:
break
# Update most consecutive matches found
longest_run = max(longest_run, count)
# After checking for runs at each character in seqeuence, return longest run found
return longest_run
main()