Skip to content

Commit 744709f

Browse files
author
Hamid Gasmi
committed
#162 is completed
1 parent bdd755c commit 744709f

File tree

2 files changed

+48
-8
lines changed
  • 5-string-processing-and-pattern-matching-algorithms/2-burrows-wheeler-suffix-arrays

2 files changed

+48
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,54 @@
11
# python3
22
import sys
33

4-
def InverseBWT(bwt):
5-
# write your code here
6-
return ""
4+
class Burrows_Wheeler_Inverse:
75

6+
def inverse(self, bwt_text):
7+
8+
last_column = [c for c in bwt_text]
9+
first_column = [c for c in bwt_text]
10+
first_column.sort()
11+
last_to_first = self.build_last_to_first(first_column, last_column)
12+
13+
origin_text_list = []
14+
origin_text_list.append(first_column[0])
15+
16+
i = 0
17+
while last_to_first[i] != 0:
18+
i = last_to_first[i]
19+
origin_text_list.append(first_column[i])
20+
21+
origin_text_list.reverse()
22+
23+
return ''.join(origin_text_list)
24+
25+
def build_last_to_first(self, first_column, last_column):
26+
last_to_first = []
27+
28+
first_occurence = dict()
29+
for i in range(len(first_column)):
30+
c = first_column[i]
31+
if not c in first_occurence:
32+
first_occurence[c] = i
33+
34+
char_count = dict()
35+
for i in range(len(last_column)):
36+
c = last_column[i]
37+
38+
if c in char_count:
39+
count = char_count[c]
40+
char_count[c] += 1
41+
else:
42+
char_count[c] = 1
43+
count = 0
44+
45+
last_to_first.append(first_occurence[c] + count)
46+
47+
return last_to_first
848

949
if __name__ == '__main__':
1050
bwt = sys.stdin.readline().strip()
11-
print(InverseBWT(bwt))
51+
52+
bwt_inverse = Burrows_Wheeler_Inverse()
53+
54+
print(bwt_inverse.inverse(bwt))
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
3
2-
ATAGA
3-
ATC
4-
GAT
1+
ATG$CAAA

0 commit comments

Comments
 (0)