Skip to content

Commit e36abda

Browse files
imp2002shermanhui
authored andcommitted
[mypy] Fix type annotations for strings (TheAlgorithms#4641)
* Fix mypy error for min_cost_string_conversion.py * Fix mypy error for manacher.py * Fix mypy error for aho_corasick.py
1 parent 924b91b commit e36abda

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

strings/aho_corasick.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44

55
class Automaton:
6-
def __init__(self, keywords: List[str]):
7-
self.adlist = list()
6+
def __init__(self, keywords: list[str]):
7+
self.adlist: list[dict] = list()
88
self.adlist.append(
99
{"value": "", "next_states": [], "fail_state": 0, "output": []}
1010
)
@@ -22,9 +22,8 @@ def find_next_state(self, current_state: int, char: str) -> Union[int, None]:
2222
def add_keyword(self, keyword: str) -> None:
2323
current_state = 0
2424
for character in keyword:
25-
if self.find_next_state(current_state, character):
26-
current_state = self.find_next_state(current_state, character)
27-
else:
25+
next_state = self.find_next_state(current_state, character)
26+
if next_state is None:
2827
self.adlist.append(
2928
{
3029
"value": character,
@@ -35,10 +34,12 @@ def add_keyword(self, keyword: str) -> None:
3534
)
3635
self.adlist[current_state]["next_states"].append(len(self.adlist) - 1)
3736
current_state = len(self.adlist) - 1
37+
else:
38+
current_state = next_state
3839
self.adlist[current_state]["output"].append(keyword)
3940

4041
def set_fail_transitions(self) -> None:
41-
q = deque()
42+
q: deque = deque()
4243
for node in self.adlist[0]["next_states"]:
4344
q.append(node)
4445
self.adlist[node]["fail_state"] = 0
@@ -68,18 +69,21 @@ def search_in(self, string: str) -> Dict[str, List[int]]:
6869
>>> A.search_in("whatever, err ... , wherever")
6970
{'what': [0], 'hat': [1], 'ver': [5, 25], 'er': [6, 10, 22, 26]}
7071
"""
71-
result = dict() # returns a dict with keywords and list of its occurrences
72+
result: dict = (
73+
dict()
74+
) # returns a dict with keywords and list of its occurrences
7275
current_state = 0
7376
for i in range(len(string)):
7477
while (
7578
self.find_next_state(current_state, string[i]) is None
7679
and current_state != 0
7780
):
7881
current_state = self.adlist[current_state]["fail_state"]
79-
current_state = self.find_next_state(current_state, string[i])
80-
if current_state is None:
82+
next_state = self.find_next_state(current_state, string[i])
83+
if next_state is None:
8184
current_state = 0
8285
else:
86+
current_state = next_state
8387
for key in self.adlist[current_state]["output"]:
8488
if not (key in result):
8589
result[key] = []

strings/manacher.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,28 @@ def palindromic_string(input_string: str) -> str:
3535
length = [1 for i in range(len(new_input_string))]
3636

3737
# for each character in new_string find corresponding palindromic string
38-
for i in range(len(new_input_string)):
39-
k = 1 if i > r else min(length[l + r - i] // 2, r - i + 1)
38+
start = 0
39+
for j in range(len(new_input_string)):
40+
k = 1 if j > r else min(length[l + r - j] // 2, r - j + 1)
4041
while (
41-
i - k >= 0
42-
and i + k < len(new_input_string)
43-
and new_input_string[k + i] == new_input_string[i - k]
42+
j - k >= 0
43+
and j + k < len(new_input_string)
44+
and new_input_string[k + j] == new_input_string[j - k]
4445
):
4546
k += 1
4647

47-
length[i] = 2 * k - 1
48+
length[j] = 2 * k - 1
4849

4950
# does this string is ending after the previously explored end (that is r) ?
5051
# if yes the update the new r to the last index of this
51-
if i + k - 1 > r:
52-
l = i - k + 1 # noqa: E741
53-
r = i + k - 1
52+
if j + k - 1 > r:
53+
l = j - k + 1 # noqa: E741
54+
r = j + k - 1
5455

5556
# update max_length and start position
56-
if max_length < length[i]:
57-
max_length = length[i]
58-
start = i
57+
if max_length < length[j]:
58+
max_length = length[j]
59+
start = j
5960

6061
# create that string
6162
s = new_input_string[start - max_length // 2 : start + max_length // 2 + 1]

strings/min_cost_string_conversion.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import List, Tuple
2-
31
"""
42
Algorithm for calculating the most cost-efficient sequence for converting one string
53
into another.
@@ -18,7 +16,7 @@ def compute_transform_tables(
1816
replace_cost: int,
1917
delete_cost: int,
2018
insert_cost: int,
21-
) -> Tuple[List[int], List[str]]:
19+
) -> tuple[list[list[int]], list[list[str]]]:
2220
source_seq = list(source_string)
2321
destination_seq = list(destination_string)
2422
len_source_seq = len(source_seq)
@@ -28,7 +26,7 @@ def compute_transform_tables(
2826
[0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
2927
]
3028
ops = [
31-
[0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
29+
["0" for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
3230
]
3331

3432
for i in range(1, len_source_seq + 1):
@@ -59,7 +57,7 @@ def compute_transform_tables(
5957
return costs, ops
6058

6159

62-
def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]:
60+
def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
6361
if i == 0 and j == 0:
6462
return []
6563
else:

0 commit comments

Comments
 (0)