forked from Shivi91/Rosalind-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path042_REAR.py
153 lines (117 loc) · 4.09 KB
/
042_REAR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
'''
A solution to a ROSALIND bioinformatics problem.
Problem Title: Reversal Distance
Rosalind ID: REAR
Rosalind #: 042
URL: http://rosalind.info/problems/rear/
'''
from copy import deepcopy
with open('data/rosalind_rear.txt') as input_data:
Permutations = [pair.strip().split('\n') for pair in input_data.read().split('\n\n')]
for index, pair in enumerate(Permutations):
Permutations[index] = [map(int, perm.split()) for perm in pair]
# with open('D:/test.txt') as input_data:
# Permutations = [pair.strip().split('\n') for pair in input_data.read().split('\n\n')]
# for index, pair in enumerate(Permutations):
# Permutations[index] = [map(int, perm.split()) for perm in pair]
count_set =[]
count_list = []
A = deepcopy(Permutations)
for fixed_perm, perm in A:
i, count = 0, 0
while i < len(fixed_perm) - 1:
if fixed_perm[i] == perm[i]:
i += 1
else:
j = perm.index(fixed_perm[i])
perm[i:j+1] = reversed(perm[i:j+1])
count += 1
count_list.append(count)
print count_list
count_set.append(count_list)
count_list = []
A = deepcopy(Permutations)
for fixed_perm, perm in A:
i, count = len(perm)-1, 0
while i > 0:
if fixed_perm[i] == perm[i]:
i -= 1
else:
j = perm.index(fixed_perm[i])
perm[j:i+1] = reversed(perm[j:i+1])
count += 1
count_list.append(count)
print count_list
count_set.append(count_list)
A = deepcopy(Permutations)
count_list = []
for fixed_perm, perm in A:
count, left_index, right_index = 0, 0, len(perm)-1
while fixed_perm != perm:
while perm[left_index] == fixed_perm[left_index]:
left_index += 1
while perm[right_index] == fixed_perm[right_index]:
right_index -= 1
left_dist = perm.index(fixed_perm[left_index]) - left_index
right_dist = right_index - perm.index(fixed_perm[right_index])
if left_dist <= right_dist :
perm[left_index:left_index+left_dist+1] = reversed(perm[left_index:left_index+left_dist+1])
count += 1
else:
perm[right_index-right_dist:right_index+1] = reversed(perm[right_index-right_dist:right_index+1])
count += 1
count_list.append(count)
print count_list
count_set.append(count_list)
A = deepcopy(Permutations)
count_list = []
for fixed_perm, perm in A:
count, left_index, right_index = 0, 0, len(perm)-1
while fixed_perm != perm:
while perm[left_index] == fixed_perm[left_index]:
left_index += 1
while perm[right_index] == fixed_perm[right_index]:
right_index -= 1
left_dist = perm.index(fixed_perm[left_index]) - left_index
right_dist = right_index - perm.index(fixed_perm[right_index])
if left_dist >= right_dist :
perm[left_index:left_index+left_dist+1] = reversed(perm[left_index:left_index+left_dist+1])
count += 1
else:
perm[right_index-right_dist:right_index+1] = reversed(perm[right_index-right_dist:right_index+1])
count += 1
count_list.append(count)
print count_list
count_set.append(count_list)
A = deepcopy(Permutations)
count_list = []
for fixed_perm, perm in A:
count, left_index, right_index = 0, 0, len(perm)-1
while fixed_perm != perm:
while perm[left_index] == fixed_perm[left_index]:
left_index += 1
while perm[right_index] == fixed_perm[right_index]:
right_index -= 1
left_dist = perm.index(fixed_perm[left_index]) - left_index
right_dist = right_index - perm.index(fixed_perm[right_index])
if left_dist + right_dist > right_index - left_index + 1:
if left_dist >= right_dist :
perm[left_index:left_index+left_dist+1] = reversed(perm[left_index:left_index+left_dist+1])
count += 1
else:
perm[right_index-right_dist:right_index+1] = reversed(perm[right_index-right_dist:right_index+1])
count += 1
elif left_dist <= right_dist :
perm[left_index:left_index+left_dist+1] = reversed(perm[left_index:left_index+left_dist+1])
count += 1
else:
perm[right_index-right_dist:right_index+1] = reversed(perm[right_index-right_dist:right_index+1])
count += 1
count_list.append(count)
print count_list
count_set.append(count_list)
min_count = [str(min(perm_count)) for perm_count in zip(*count_set)]
print ' '.join(min_count)
with open('output/042_REAR.txt', 'w') as output_data:
output_data.write(' '.join(min_count))