-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathburrows-wheeler-transform.py
51 lines (47 loc) · 1.33 KB
/
burrows-wheeler-transform.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
"""
Burrows Wheeler Transform
"""
def leftrotate(s, d):
tmp = s[d : ] + s[0 : d]
return tmp
def bwt(input):
input += "$"
output = ""
rotations = []
for i in range(len(input)):
rotation = leftrotate(input, i)
rotations.append(rotation)
rotations = sorted(rotations)
for r in rotations:
output += r[len(r)-1]
print(output)
def invbwt(input):
sortedBST = sorted(input)
last = []
first = []
countLast = {}
countFirst = {}
for i in range(len(sortedBST)):
if(input[i] not in countLast.keys()):
countLast.update({input[i]: 1})
else:
countLast.update({input[i]: countLast[input[i]] + 1})
if(sortedBST[i] not in countFirst.keys()):
countFirst.update({sortedBST[i]: 1})
else:
countFirst.update({sortedBST[i]: countFirst[sortedBST[i]] + 1})
last.append((input[i], countLast[input[i]]))
first.append((sortedBST[i], countFirst[sortedBST[i]]))
firstToLast = []
for elt in last:
firstToLast.append(first.index(elt))
print("First To Last: ", firstToLast)
output = ""
elt = ("$", 1)
for i in range(len(last)):
index = last.index(elt)
output += first[index][0]
elt = first[index]
print(output)
bwt("CPLUSPLUS")
invbwt("S$PPSCUULL")