-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplement BWMatching.py
More file actions
74 lines (62 loc) · 1.93 KB
/
Implement BWMatching.py
File metadata and controls
74 lines (62 loc) · 1.93 KB
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
def getN(ch, row, column):
n = 0
i = 0
while i <= row:
if ch == column[i]:
n += 1
i += 1
return n
def get_char(ch, column, seq):
pos = 0
count = 0
for i in range(len(column)):
if column[i] == ch:
pos = i
count += 1
if count == seq:
return pos, count
def lastToFirst(Transform, i):
last = Transform
first = sorted(Transform)
ch = last[i]
n = getN(ch,i,last)
pos,count = get_char(ch,first,n)
return pos
def bw_match(LastColumn, Patterns):
def LastColumnContains(symbol, top, bottom):
topIndex = None
bottomIndex = None
N = len(LastColumn)
for i in range(top, N):
if LastColumn[i] == symbol:
bottomIndex = i
break
for i in range(bottom, -1, -1):
if LastColumn[i] == symbol:
topIndex = i
break
return topIndex, bottomIndex
def match(Pattern):
top = 0
bottom = len(LastColumn) - 1
while top <= bottom:
if len(Pattern) > 0:
symbol = Pattern[-1]
Pattern = Pattern[:-1]
topIndex, bottomIndex = LastColumnContains(symbol, top, bottom)
if type(topIndex) == int and type(bottomIndex) == int:
top = lastToFirst(LastColumn, bottomIndex)
bottom = lastToFirst(LastColumn, topIndex)
else:
return 0
else:
return bottom - top + 1
return 0
return [match(Pattern) for Pattern in Patterns]
file = open("C:/Users/iMan/desktop/rosalind_ba9l.txt","r")
text = file.read()
strings = []
strings = text.split("\n")
Result = bw_match(strings[0], strings[1].split())
output = ' '.join(str(i) for i in Result)
print(output)