Problem
Figure 2. Palindromic recognition site
A DNA string is a reverse palindrome if it is equal to its reverse complement. For instance, GCATGC is a reverse palindrome because its reverse complement is GCATGC. See Figure 2.
Given: A DNA string of length at most 1 kbp in FASTA format.
Return: The position and length of every reverse palindrome in the string having length between 4 and 12. You may return these pairs in any order.
如果DNA字符串与其反向互补序列相等,则为反向回文序列。例如,GCATGC是反向回文,因为其反向互补是GCATGC。参见图2。
返回:该位置和长度每反向回文在具有长度的字符串4和12之间,您可以以任何顺序返回这些对。
Sample Dataset
>Rosalind_24 TCAATGCATGCGGGTCTATATGCAT
Sample Output
4 6 5 4 6 6 7 4 17 4 18 4 20 6 21 4
python解决方案
dna=input()
dna_com=''
for i in dna:
if i=='A':
dna_com+='T'
elif i=='C':
dna_com+='G'
elif i=='G':
dna_com+='C'
else:
dna_com+='A'
for i in range(len(dna)-4+1):
j=4
while j<=12:
if dna[i:i+j]==(dna_com[i:i+j])[::-1]:
if i + j < len(dna):
print(i + 1, j)
else:
print(i + 1, len(dna) - i)#当i+j的长度超过原字符串的时候,要注意长度的变化
i+=1
j+=1