If you want something to think about, analyse this:
def GetMultipleInString(dna, term):
# computing end condition 0
if (term not in dna):
print (dna + " does not contain the term " + term)
return []
# start of list of lists of 2 elements: index, rest
result = [[None,dna]]
# we look for the index in the rest, need to keep track how much we
# shortened the string in total so far to get index in complete string
totalIdx = 0
# we look at the last element of the list until it's length is shorter
# than the term we look for (end of computing condition 1)
termLen = len(term)
while len(result[-1][1]) >= termLen:
# get the last element
last = result[-1][1]
try:
# find our term, if not found -> exception
idx = last.index(term)
# partition "abcdefg" with "c" -> ("ab","c", "defg")
# we take only the remaining
rest = last.partition(term)[2]
# we compute the total index, and put it in our result
result.append( [idx+totalIdx , rest] )
totalIdx += idx+termLen
except:
result.append([None,last])
break
# any results found that are not none?
if (any( x[0] != None for x in result)):
print (dna + " contains the term " + term + " at positions:"),
# get only indexes from our results
rv = [ str(x[0]) for x in result if x[0] != None]
print (' '.join(rv))
return rv
else:
print (dna + " does not contain the term " + term)
return []
print("_----------------------------------_")
myDna = "AATGCATGC"
res1 = GetMultipleInString(myDna,"ATG")
print(res1)
res2 = GetMultipleInString(myDna,"A")
print(res2)