Last active
December 11, 2015 22:38
-
-
Save calizarr/4670646 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Reversal Distance | |
dataset = open('rosalind_rear_sample.txt','r') | |
data = dataset.read().split('\n') | |
dataset.close() | |
while len(data)>(len(data)-data.count('')): | |
data.remove('') | |
originals = [[int(x) for x in y.split()] for y in data[::2]] | |
perms = [[int(x) for x in y.split()] for y in data[1::2]] | |
def findBreak(perm): | |
num = 0 | |
brkptspots = [] | |
for index in range(len(perm)-1): | |
breakpoint = abs(perm[index+1]-perm[index]) | |
if index+1==len(perm)-1 and breakpoint!=1: | |
num+=1 | |
brkptspots.append(index),brkptspots.append(index+1) | |
elif breakpoint!=1: | |
num+=1 | |
brkptspots.append(index) | |
return num, brkptspots | |
def possRev(perm): | |
revs = [] | |
bpis = [] | |
bpi,brkpts=findBreak(perm) | |
for first in brkpts: | |
for second in brkpts[brkpts.index(first)+1:]: | |
if first==0: | |
continue | |
elif second-first==1: | |
continue | |
else: | |
perm[first:second]=reversed(perm[first:second]) | |
checkBpi(perm,bpi,revs,bpis) | |
perm[first:second]=reversed(perm[first:second]) | |
mBpi=min(bpis) | |
if len(revs)==0: | |
return None | |
else: | |
return pruneRevs(revs,mBpi) | |
def flatten(reversals): | |
lst=[] | |
for rev in reversals: lst.extend(rev) | |
return lst | |
def pruneRevs(reversals,mBpi): | |
revs = [] | |
totRevs = [] | |
for rev in reversals: | |
if findBreak(rev)[0]==mBpi: | |
revs.append(rev) | |
return revs | |
def checkBpi(perm,bpi,revs,bpis): | |
bpi2=findBreak(perm)[0] | |
if bpi-bpi2==2: | |
revs.append(perm[:]) | |
bpis.append(bpi2) | |
elif bpi-bpi2==1: | |
revs.append(perm[:]) | |
bpis.append(bpi2) | |
elif bpi-bpi2==0: | |
revs.append(perm[:]) | |
bpis.append(bpi2) | |
s = perms[3][:] | |
s.insert(0,0),s.append(11) | |
count=1 | |
s=possRev(s) | |
w=[0,1,2,3,4,5,6,7,8,9,10,11] | |
q=[4,3,2,1,8,9,10,7,6,5] | |
out = open('rosalind_rear_out.txt','w') | |
out.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment