tl;dr: Code seems to run perfectly, check50/Submit50, however, is timing out before a response is provided.
I have manually checked all test cases provided on the Pset page: small.csv 1.txt through to large.csv 21.txt and I get the correct answer. It just seems to be an efficiency issue.
I've stripped it down as best I can, but check50 keeps timing out.
from sys import argv, exit
import sys
import csv
# checks for 2 command lines exactly
if len(argv) < 3:
print("Usage: python dna.py data.csv sequence.txt")
exit(0)
# open csv file as a list
csv_file = open(sys.argv[1], newline='')
csvreader = csv.reader(csv_file)
row1 = next(csvreader)
txt_file = open(sys.argv[2], "r")
seqreader = txt_file.read()
#print(seqreader)
# Create empty list
counter = []
# Iterate through DNA
for i in range(1, len(row1)):
occurance = 0
n = 1
while row1[i]*n in seqreader:
n += 1
if (n - 1) > occurance:
occurance = n - 1
counter.append(occurance)
#print(occurance)
counterlen = len(counter)
# Check if no match
match = False
for row in csvreader:
for c in range(counterlen):
# row itteration
if int(row[c+1]) != int(counter[c]):
break
else:
print(row[0])
match = True
break
# if no match
if match == False:
print("No match")
txt_file.close()
csv_file.close()
Any suggestions?
EDIT: After some tinkering, it seems like I am doing unnecessary iterations, still at a loss as to how I can prevent this...