I have really tried to just solve this one myself but after a few weeks this seems futile without guidance. I am having trouble getting a for loop to calculate the maximum consecutive occurrences of each substring (str).

if len(sys.argv) != 3:
    print("Usage: python dna.py data.csv sequence.txt")

strCounts = {}
strList = []
dna_seq = ""
database = {}

#open csv file
with open(sys.argv[1], "r") as csvfile:
    reader = csv.DictReader(csvfile)
    database = list(reader)

#generate list of all present str codes
    strList = list(reader.fieldnames)

#open sequence txt file
with open(sys.argv[2], "r") as f:
    dna_seq = f.read()

#Loop through dna sequence searching for each specified str
for i in range(len(strList)):
    target = strList[i]
    window = len(target)
#   print("The current target str is: " + strList[i] + " and it's window length is " + str(window))

#for loop to count max consecutive occurences of each str
    max_count = 0
    count = 0
    for k in range (len(dna_seq)):
        if dna_seq[k : window-1] == target:
          count +=1
          k = window
          window = window + window
          if max_count == None or max_count < count:
             max_count = count
        else: window +=1

    strCounts[target] = max_count

I had initially set k to k += 1 and same for window. However logic suggests the window should slide the full distance of the substring(window) if there is a match

1 Answer 1


For anyone interested I was able to use the method described here - https://www.geeksforgeeks.org/python-maximum-consecutive-substring-occurrence/

I then divided result by the len of each substring (str) to give the max number of occurrences

You must log in to answer this question.

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