Rosalind 42-Reversal Distance

Bioinformatics Stronghold 第42题Reversal Distance,本题给定几组排列的集合,计算每一组的两个排列的反转距离,就是由每一组集合中上一个集合转变为下一个集合的最小反转次数

Rosalind 42-Reversal Distance

代码:

def rear(path):
    with open(path) as fp:
        r=fp.read().split('\n')
        r=[tuple(r[i].split())for i in range(len(r)) if r[i]!='']
    return [reversal_distance({r[i]},{r[i+1]}) for i in range(0,len(r)-1,2)]
def get_reversal(s):
    for i in range(len(s)):
        for j in range(i+2,len(s)+1):
            yield s[:i]+s[i:j][::-1]+s[j:]
def reversal_distance(s,t,distance=0):
    if s & t:return distance
    new_s=set(tuple(j) for i in s for j in get_reversal(i))
    new_t=set(tuple(j) for i in t for j in get_reversal(i))
    distance+=2
    if s & new_t:return distance-1
    if t & new_s:return distance-1
    if new_s&new_t:return distance
    distance=reversal_distance(new_s,new_t,distance)
    return distance
print(rear(r'BIO\rosalind\42.txt'))

链接:http://rosalind.info/problems/rear/

上一篇:42.查找


下一篇:Effective C++条款42:模板与泛型编程——了解typename的双重意义