You can build a finite state machine with only 2 states to solve your problem: / p>
def pesquisar( seq, inicio, fim ):
estado = 0
ret = []
aux = []
for x in seq:
if estado == 0:
if x == inicio:
aux = [ x ]
estado = 1
elif estado == 1:
aux.append( x );
if x == fim:
ret.append(aux)
estado = 0
return ret
sequencia = ['CGC','UUC','GCU','UUG','GAA','AAU','UUG','UGU','GUU','UUU','UGU','GGC','UGC','UCG','CUG','CUC','AAA','UUG','UUC','GCU','GCU','UUU','UGU','GUC','CUG','GCU','GCU','UUU','AUU','AUU','UUA','CGC','UGC','UUG','GCG','CUG','CUY','UUA','CGC','UGC','UUG','GGC','UUG','UUG','UGG','CUU','UGG','UUG','UUU','GUU','UAU','UAY','GCU','GCU','CUU','GUU','GUU','GUU','GCU','UGU','UGU','GCC','UAU','GGC']
print(pesquisar( sequencia, inicio = 'UGU', fim = 'UGC' ))
Output:
[['UGU', 'GUU', 'UUU', 'UGU', 'GGC', 'UGC'],
['UGU', 'GUC', 'CUG', 'GCU', 'GCU', 'UUU', 'AUU', 'AUU', 'UUA', 'CGC', 'UGC']]
EDIT:
State machines can be built on Python
with the use of yield
, there is an alternative way to solve the problem with an even more compact code:
def pesquisar( seq, inicio, fim ):
ret = []
for i in seq:
if i == inicio or ret:
ret.append(i)
if i == fim and ret:
yield ret
ret = []
sequencia = ['CGC','UUC','GCU','UUG','GAA','AAU','UUG','UGU','GUU','UUU','UGU','GGC','UGC','UCG','CUG','CUC','AAA','UUG','UUC','GCU','GCU','UUU','UGU','GUC','CUG','GCU','GCU','UUU','AUU','AUU','UUA','CGC','UGC','UUG','GCG','CUG','CUY','UUA','CGC','UGC','UUG','GGC','UUG','UUG','UGG','CUU','UGG','UUG','UUU','GUU','UAU','UAY','GCU','GCU','CUU','GUU','GUU','GUU','GCU','UGU','UGU','GCC','UAU','GGC']
print(list(pesquisar( sequencia, inicio = 'UGU', fim = 'UGC')))
Output:
[['UGU', 'GUU', 'UUU', 'UGU', 'GGC', 'UGC'],
['UGU', 'GUC', 'CUG', 'GCU', 'GCU', 'UUU', 'AUU', 'AUU', 'UUA', 'CGC', 'UGC']]