-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgreedy.py
222 lines (189 loc) · 5.78 KB
/
greedy.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import fileinput, logging, sys, time
"""
To access the results of running this code, please run "make" on the terminal.
This is the correct version of the code; please refer to it when analyzing my
algorithm.
The first method (sequence) does most of the work. Following it are several methods helping
it to fnd overlaps. This is a slow (greedy) version of the algorithm with no optimizations.
"""
def sequence(lst):
def sequence_helper(lst):
"""
The main sequencing function. Will call itself recursively.
"""
#print(lst)
#Base case. What if we have 2 elements?
while len(lst) > 0:
if len(lst) == 2:
return overlap(lst[0],lst[1])
elif len(lst) == 1:
return lst[0]
elif len(lst) == 0:
return ""
else:
element = lst[0]
winning_score = -1
winner = ""
for candidate in lst[1:]:
score = amount_of_overlap(element,candidate)
if score > winning_score:
winning_score = score
winner = candidate
lst[0] = overlap(element,winner)
lst.remove(winner)
#print(lst)
#print("")
#return sequence_helper(lst)
if len(lst) == 2:
return overlap(lst[0],lst[1])
elif len(lst) == 1:
return lst[0]
elif len(lst) == 0:
#print("No way!!!")
return ""
else:
#print(lst)
#print("")
winning_score = -1
winner = ( )
for i in range(len(lst)):
for j in range(i + 1, len(lst)):
score = amount_of_overlap(lst[i],lst[j])
if score > winning_score:
winning_score = score
winner = ( lst[i],lst[j] )
for k in winner:
lst.remove(k)
#return
lst = [ overlap(winner[0],winner[1]) ] + lst
#print(lst)
#print("")
return sequence_helper(lst)
"""
--------------------------------------------------------------------------------------------
FORTRESS OF OVERLAPS
--------------------------------------------------------------------------------------------
"""
def amount_of_overlap(str1,str2):
def amt_helper(str1,str2,counter):
#Testing overlap between ends.
overlap = ""
for i in range(min(len(str1),len(str2))):
overlap1 = str1[0:i + 1]
overlap2 = str2[len(str2) - 1 - i:len(str2)]
if (overlap1 != overlap2):
continue
else:
overlap = overlap1
#Testing inclusion of one sequence in another.
hug = ""
hugger = ""
if counter == 0:
#Testing inclusion of one sequence in another.
shortest_string = str1 if len(str1) < len(str2) else str2
longest_string = str2 if len(str1) < len(str2) else str1
for i in range(len(longest_string) - len(shortest_string) + 1):
if longest_string[i:i + len(shortest_string)] == shortest_string:
hug = shortest_string
hugger = longest_string
break
return max(len(overlap),len(hug))
return max(amt_helper(str1,str2,0),amt_helper(str2,str1,1))
def overlap(str1,str2):
"""
Determines the smallest string possible created from str1 and str2, squishing overlaps.
Runs in O(k) time.
"""
def overlap_helper(str1,str2,counter):
"""
A helper function that determines the maximum overlap in a specific order (laziness).
"""
#Testing overlap between ends.
overlap = ""
max_i = -1
for i in range(min(len(str1),len(str2))):
overlap1 = str1[0:i + 1]
overlap2 = str2[len(str2) - 1 - i:len(str2)]
if (overlap1 != overlap2):
continue
else:
overlap = overlap1
max_i = i
hug = ""
hugger = ""
if counter == 0:
#Testing inclusion of one sequence in another.
shortest_string = str1 if len(str1) < len(str2) else str2
longest_string = str2 if len(str1) < len(str2) else str1
for i in range(len(longest_string) - len(shortest_string) + 1):
if longest_string[i:i + len(shortest_string)] == shortest_string:
hug = shortest_string
hugger = longest_string
break
#return max(len(overlap),len(hug))
#return overlap
if len(overlap) >= len(hug):
return str2[0:len(str2) - 1 - max_i] + overlap + str1[max_i + 1:len(str1)]
else:
#print("Hugger territory")
#print(hug)
#print(hugger)
#print("")
return hugger
order1 = overlap_helper(str1,str2,0)
order2 = overlap_helper(str2,str1,1)
if len(order1) < len(order2):
return order1
else:
return order2
"""
--------------------------------------------------------------------------------------------
********************************************MAIN********************************************
--------------------------------------------------------------------------------------------
"""
def main():
#A class to let me print time to the terminal.
class Logger(object):
def __init__(self):
self.log = open("logfile.txt", "w")
def write(self, message):
self.log.write(message)
starttime = time.time()
#Create a list of all lines in the input file, which will be fed into the algorithm.
alg_input = []
for line in fileinput.input():
line = line.strip() # Remove the trailing newline
alg_input.append(line)
global MEMO, OVERLAP_MEMO #MEMO = shortest total sring, OVERLAP_MEMO = max total overlap
MEMO = {}
OVERLAP_MEMO = {}
try:
print(sequence(alg_input))
except RuntimeError:
print("ACGT")
sys.stdout = Logger()
print('Solution found in %.1f seconds.' % (time.time() - starttime))
if __name__ == "__main__":
main()
"""
--------------------------------------------------------------------------------------------
**************************************RECYCLING BIN*****************************************
--------------------------------------------------------------------------------------------
Why hello there. You've reached my Recycling Bin!
---
#Str1 comes first
ptr1_loc = len(str1) - 1
ptr2_loc = 0
ptr1 = str1[ptr1_loc]
ptr2 = str2[ptr2_loc]
while ptr1 != ptr2 and ptr1_loc >= 0:
#Finds the first location where str1 and
ptr1_loc -= 1
ptr1 = str1[ptr1_loc]
#Str2 comes first
ptr1_loc =
ptr2_loc = len(str2) - 1
ptr1 = str1[ptr1_loc]
ptr2 = str2[ptr2_loc]
---
"""