0

The code runs for all 20 sequences on the terminal but during submit50/check50, it runs into an error with the following log:

enter image description here

the code is attached for reference.

import csv
import sys

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Usage: python dna.py [csv file] [sequence file]")
        sys.exit()
dna_database = []
count_data = []
with open(sys.argv[1], 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    for row in csvreader:
        dna_database.append((row))

with open(sys.argv[2], 'r') as sequencefile:
    data = sequencefile.read()

for i in range(1, len(dna_database[0])):
    count = 0
    index = data.find(dna_database[0][i])
    if index > -1:
        # incrementing one count for first finding in each sequence of pattern
        local_count = 1

    while index > -1:

        if data.find(dna_database[0][i], index+1) - data.find(dna_database[0][i], index) == len(dna_database[0][i]):
            local_count += 1

        else:
            if local_count > count:
                count = local_count
            # incrementing one count for first finding in each sequence of pattern, hence local_count should start from 1
            local_count = 1

        index = data.find(dna_database[0][i], index + 1)

    count_data.append(str(count))

# navigating through all the rows in list for checking the pattern
for i in range(1, len(dna_database)):

    if dna_database[i][1::] == count_data:
        print("{}".format(dna_database[i][0]))
        sys.exit()

print("No match")

I am working in pset6/dna/ directory with code in file dna.py. Please suggest what can be the issue.

0

I would like to post it as a comment but because there is a lot of code and it would be hard to explain i chose to write it here:

I just changed you code a little bit as you can see below and it made the validation printing Usage: python dna.py [csv file] [sequence file] when there are not 3 arguments in sys.argv, a No match message when I tested using the python dna.py databases/large.csv sequences/4.txt command and also printed Fred when typing python dna.py databases/large.csv sequences/19.txt

import csv
import sys

def main():
    
    if len(sys.argv) != 3:
        print("Usage: python dna.py [csv file] [sequence file]")
        sys.exit()
        
    dna_database = []
    count_data = []
    with open(sys.argv[1], 'r') as csvfile:
        csvreader = csv.reader(csvfile)
        for row in csvreader:
            dna_database.append((row))
    
    with open(sys.argv[2], 'r') as sequencefile:
        data = sequencefile.read()
    
    for i in range(1, len(dna_database[0])):
        count = 0
        index = data.find(dna_database[0][i])
        if index > -1:
            # incrementing one count for first finding in each sequence of pattern
            local_count = 1
    
        while index > -1:
    
            if data.find(dna_database[0][i], index+1) - data.find(dna_database[0][i], index) == len(dna_database[0][i]):
                local_count += 1
    
            else:
                if local_count > count:
                    count = local_count
                # incrementing one count for first finding in each sequence of pattern, hence local_count should start from 1
                local_count = 1
    
            index = data.find(dna_database[0][i], index + 1)
    
        count_data.append(str(count))
    
    # navigating through all the rows in list for checking the pattern
    for i in range(1, len(dna_database)):
    
        if dna_database[i][1::] == count_data:
            print("{}".format(dna_database[i][0]))
            sys.exit()
    
    print("No match")

if __name__ == '__main__':
    main()
| improve this answer | |

You must log in to answer this question.

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