I have been stuck with last part of the code that is supposed to match the DNA sequence from sequence.txt with the one in database.csv. It passes most of the tests in check50 and fails only in sequence 3 and 18.
I read database.csv into a dictionary and use the for loop to iterate through each row of dictionary. Then another loop compares value of each STR from database with STR values stored in a list called "pattern" and with every match it adds 1 to the counter. If counter equals the length of STR it means that all values in this segment are matching and therefor it prints the "name" of the row that has a match and exits.
with open(sys.argv, "r") as fl: dictionary = csv.DictReader(fl) counter = 0 for row in dictionary: for k in range(len(STR)): if int(row[STR[k]]) == pattern[k]: counter += 1 if counter == len(STR): print(row["name"]) exit() print("No match")`
I feel it would make more sense to store the results of "repeat_count" function in dictionary to compare dictionary vs dictionary in the end but I failed to implement it. Any thoughts what am I missing in lines 25-36 to match all the sequences correctly? Or maybe the right approach would be to store the number of repeats assigned to STRs within a dictionaries?
#import libraries import csv import sys def main(): # ensure correct amount of arguments if len(sys.argv) != 3: sys.exit("Usage: python dna.py database.csv sequence.txt ") # open CSV file and read it into memory with open(sys.argv, "r") as file: database = csv.reader(file) STR =  for row in database: for i in range(len(row)): if i != 0: STR.append(row[i]) break # open text file and read it into memory with open(sys.argv, "r") as f: sequence = f.read() # for each STR compute the longest run of consecutive repeats of DNA sequences and store it in a list pattern =  for i in STR: pattern.append(repeat_count(sequence,i)) # compare the list with pattern to a database with open(sys.argv, "r") as fl: dictionary = csv.DictReader(fl) counter = 0 for row in dictionary: for k in range(len(STR)): if int(row[STR[k]]) == pattern[k]: counter += 1 if counter == len(STR): print(row["name"]) exit() print("No match") create a function that checks DNA sequence for consecutive pattern repeats and returns the maximum value of repeats def repeat_count(sequence, STR): # initialize counter counter = 0 # create a list to store all the values of repeats counters =  i = 0 j = len(STR) # create a loop that iterates through substrings and adds results to the list while j <= len(sequence): if STR == sequence[i:j]: counter += 1 i = i + len(STR) j = j + len(STR) else: counters.append(counter) counter = 0 i = i + 1 j = j + 1 # return max value from the list return max(counters) if __name__ == "__main__": main()