Skip to content

Commit cf3df56

Browse files
committed
Added changes suggested by cclauss
1 parent c08c4b5 commit cf3df56

File tree

1 file changed

+70
-59
lines changed

1 file changed

+70
-59
lines changed

compression/burrows_wheeler.py

Lines changed: 70 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,46 @@
1010
original character. The BWT is thus a "free" method of improving the efficiency
1111
of text compression algorithms, costing only some extra computation.
1212
"""
13+
from typing import List, Dict
1314

1415

15-
def all_rotations(string):
16+
def all_rotations(s: str) -> List[str]:
1617
"""
17-
:param str string: The string that will be rotated len(string) times.
18-
:return: A list with len(string) rotations of the parameter string.
19-
:rtype: list[str]
20-
:raises TypeError: If the string parameter type is not str.
18+
:param s: The string that will be rotated len(s) times.
19+
:return: A list with the rotations.
20+
:raises TypeError: If s is not an instance of str.
2121
Examples:
2222
23-
>>> all_rotations("^BANANA|")
24-
['^BANANA|', 'BANANA|^', 'ANANA|^B', 'NANA|^BA', 'ANA|^BAN', 'NA|^BANA',\
25-
'A|^BANAN', '|^BANANA']
26-
>>> all_rotations("a_asa_da_casa")
27-
['a_asa_da_casa', '_asa_da_casaa', 'asa_da_casaa_', 'sa_da_casaa_a',\
28-
'a_da_casaa_as', '_da_casaa_asa', 'da_casaa_asa_', 'a_casaa_asa_d',\
29-
'_casaa_asa_da', 'casaa_asa_da_', 'asaa_asa_da_c', 'saa_asa_da_ca',\
30-
'aa_asa_da_cas']
31-
>>> all_rotations("panamabanana")
32-
['panamabanana', 'anamabananap', 'namabananapa', 'amabananapan',\
33-
'mabananapana', 'abananapanam', 'bananapanama', 'ananapanamab',\
34-
'nanapanamaba', 'anapanamaban', 'napanamabana', 'apanamabanan']
23+
>>> all_rotations("^BANANA|") # doctest: +NORMALIZE_WHITESPACE
24+
['^BANANA|', 'BANANA|^', 'ANANA|^B', 'NANA|^BA', 'ANA|^BAN', 'NA|^BANA',
25+
'A|^BANAN', '|^BANANA']
26+
>>> all_rotations("a_asa_da_casa") # doctest: +NORMALIZE_WHITESPACE
27+
['a_asa_da_casa', '_asa_da_casaa', 'asa_da_casaa_', 'sa_da_casaa_a',
28+
'a_da_casaa_as', '_da_casaa_asa', 'da_casaa_asa_', 'a_casaa_asa_d',
29+
'_casaa_asa_da', 'casaa_asa_da_', 'asaa_asa_da_c', 'saa_asa_da_ca',
30+
'aa_asa_da_cas']
31+
>>> all_rotations("panamabanana") # doctest: +NORMALIZE_WHITESPACE
32+
['panamabanana', 'anamabananap', 'namabananapa', 'amabananapan',
33+
'mabananapana', 'abananapanam', 'bananapanama', 'ananapanamab',
34+
'nanapanamaba', 'anapanamaban', 'napanamabana', 'apanamabanan']
3535
>>> all_rotations(5)
3636
Traceback (most recent call last):
3737
...
38-
TypeError: The parameter string type must be str.
38+
TypeError: The parameter s type must be str.
3939
"""
40-
if not (type(string) is str):
41-
raise TypeError("The parameter string type must be str.")
40+
if not isinstance(s, str):
41+
raise TypeError("The parameter s type must be str.")
4242

43-
return [string[i:] + string[:i] for i in range(len(string))]
43+
return [s[i:] + s[:i] for i in range(len(s))]
4444

4545

46-
def bwt_transform(string):
46+
def bwt_transform(s: str) -> Dict:
4747
"""
48-
:param str string: The string that will be used at bwt algorithm
49-
:return: A dictionary with the bwt result, the string composed of the last
50-
char of each row of the ordered rotations list and the index of the
51-
original string at ordered rotations list
52-
:rtype: dict
53-
:raises TypeError: If the string parameter type is not str
54-
:raises ValueError: If the string parameter is empty
48+
:param s: The string that will be used at bwt algorithm
49+
:return: the string composed of the last char of each row of the ordered
50+
rotations and the index of the original string at ordered rotations list
51+
:raises TypeError: If the s parameter type is not str
52+
:raises ValueError: If the s parameter is empty
5553
Examples:
5654
5755
>>> bwt_transform("^BANANA")
@@ -63,38 +61,38 @@ def bwt_transform(string):
6361
>>> bwt_transform(4)
6462
Traceback (most recent call last):
6563
...
66-
TypeError: The parameter string type must be str.
64+
TypeError: The parameter s type must be str.
6765
>>> bwt_transform('')
6866
Traceback (most recent call last):
6967
...
70-
ValueError: The parameter string must not be empty.
68+
ValueError: The parameter s must not be empty.
7169
"""
72-
if not (type(string) is str):
73-
raise TypeError("The parameter string type must be str.")
74-
if not string:
75-
raise ValueError("The parameter string must not be empty.")
70+
if not isinstance(s, str):
71+
raise TypeError("The parameter s type must be str.")
72+
if not s:
73+
raise ValueError("The parameter s must not be empty.")
7674

77-
rotations = all_rotations(string)
75+
rotations = all_rotations(s)
7876
rotations.sort() # sort the list of rotations in alphabetically order
7977
# make a string composed of the last char of each rotation
8078
return {
8179
"bwt_string": "".join([word[-1] for word in rotations]),
82-
"idx_original_string": rotations.index(string),
80+
"idx_original_string": rotations.index(s),
8381
}
8482

8583

86-
def reverse_bwt(bwt_string, idx_original_string):
84+
def reverse_bwt(bwt_string: str, idx_original_string: int) -> str:
8785
"""
88-
:param str bwt_string: The string returned from bwt algorithm execution
89-
:param int idx_original_string: The index of the string that was used to
86+
:param bwt_string: The string returned from bwt algorithm execution
87+
:param idx_original_string: A 0-based index of the string that was used to
9088
generate bwt_string at ordered rotations list
9189
:return: The string used to generate bwt_string when bwt was executed
92-
:rtype str
9390
:raises TypeError: If the bwt_string parameter type is not str
9491
:raises ValueError: If the bwt_string parameter is empty
9592
:raises TypeError: If the idx_original_string type is not int or if not
96-
possible to cast it to int
97-
:raises ValueError: If the idx_original_string value is lower than 0
93+
possible to cast it to int
94+
:raises ValueError: If the idx_original_string value is lower than 0 or
95+
greater than len(bwt_string) - 1
9896
9997
>>> reverse_bwt("BNN^AAA", 6)
10098
'^BANANA'
@@ -110,33 +108,49 @@ def reverse_bwt(bwt_string, idx_original_string):
110108
Traceback (most recent call last):
111109
...
112110
ValueError: The parameter bwt_string must not be empty.
113-
>>> reverse_bwt("mnpbnnaaaaaa", "asd")
111+
>>> reverse_bwt("mnpbnnaaaaaa", "asd") # doctest: +NORMALIZE_WHITESPACE
114112
Traceback (most recent call last):
115113
...
116-
TypeError: The parameter idx_original_string type must be int or passive of cast to int.
114+
TypeError: The parameter idx_original_string type must be int or passive
115+
of cast to int.
117116
>>> reverse_bwt("mnpbnnaaaaaa", -1)
118117
Traceback (most recent call last):
119118
...
120119
ValueError: The parameter idx_original_string must not be lower than 0.
120+
>>> reverse_bwt("mnpbnnaaaaaa", 12) # doctest: +NORMALIZE_WHITESPACE
121+
Traceback (most recent call last):
122+
...
123+
ValueError: The parameter idx_original_string must be lower than
124+
len(bwt_string).
121125
>>> reverse_bwt("mnpbnnaaaaaa", 11.0)
122126
'panamabanana'
123127
>>> reverse_bwt("mnpbnnaaaaaa", 11.4)
124128
'panamabanana'
125129
"""
126-
if not (type(bwt_string) is str):
130+
if not isinstance(bwt_string, str):
127131
raise TypeError("The parameter bwt_string type must be str.")
128132
if not bwt_string:
129133
raise ValueError("The parameter bwt_string must not be empty.")
130134
try:
131135
idx_original_string = int(idx_original_string)
132136
except ValueError:
133137
raise TypeError(
134-
"The parameter idx_original_string type must be int or passive of cast to int."
138+
(
139+
"The parameter idx_original_string type must be int or passive"
140+
" of cast to int."
141+
)
135142
)
136143
if idx_original_string < 0:
137144
raise ValueError(
138145
"The parameter idx_original_string must not be lower than 0."
139146
)
147+
if idx_original_string >= len(bwt_string):
148+
raise ValueError(
149+
(
150+
"The parameter idx_original_string must be lower than"
151+
" len(bwt_string)."
152+
)
153+
)
140154

141155
ordered_rotations = [""] * len(bwt_string)
142156
for x in range(len(bwt_string)):
@@ -147,19 +161,16 @@ def reverse_bwt(bwt_string, idx_original_string):
147161

148162

149163
if __name__ == "__main__":
150-
string = input("Provide a string that I will generate its BWT transform: ")
151-
result = bwt_transform(string)
152-
print(
153-
"Burrows Wheeler tranform for string '{}' results in '{}'".format(
154-
string, result["bwt_string"]
155-
)
156-
)
164+
entry_msg = "Provide a string that I will generate its BWT transform: "
165+
s = input(entry_msg).strip()
166+
result = bwt_transform(s)
167+
bwt_output_msg = "Burrows Wheeler tranform for string '{}' results in '{}'"
168+
print(bwt_output_msg.format(s, result["bwt_string"]))
157169
original_string = reverse_bwt(
158170
result["bwt_string"], result["idx_original_string"]
159171
)
160-
print(
161-
(
162-
"Reversing Burrows Wheeler tranform for entry '{}' we get original"
163-
" string '{}'"
164-
).format(result["bwt_string"], original_string)
172+
fmt = (
173+
"Reversing Burrows Wheeler tranform for entry '{}' we get original"
174+
" string '{}'"
165175
)
176+
print(fmt.format(result["bwt_string"], original_string))

0 commit comments

Comments
 (0)