mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
[mypy] Fix type annotations for strings (#4641)
* Fix mypy error for min_cost_string_conversion.py * Fix mypy error for manacher.py * Fix mypy error for aho_corasick.py
This commit is contained in:
parent
78a5d3a558
commit
5e7eed610c
|
@ -3,8 +3,8 @@ from typing import Dict, List, Union
|
|||
|
||||
|
||||
class Automaton:
|
||||
def __init__(self, keywords: List[str]):
|
||||
self.adlist = list()
|
||||
def __init__(self, keywords: list[str]):
|
||||
self.adlist: list[dict] = list()
|
||||
self.adlist.append(
|
||||
{"value": "", "next_states": [], "fail_state": 0, "output": []}
|
||||
)
|
||||
|
@ -22,9 +22,8 @@ class Automaton:
|
|||
def add_keyword(self, keyword: str) -> None:
|
||||
current_state = 0
|
||||
for character in keyword:
|
||||
if self.find_next_state(current_state, character):
|
||||
current_state = self.find_next_state(current_state, character)
|
||||
else:
|
||||
next_state = self.find_next_state(current_state, character)
|
||||
if next_state is None:
|
||||
self.adlist.append(
|
||||
{
|
||||
"value": character,
|
||||
|
@ -35,10 +34,12 @@ class Automaton:
|
|||
)
|
||||
self.adlist[current_state]["next_states"].append(len(self.adlist) - 1)
|
||||
current_state = len(self.adlist) - 1
|
||||
else:
|
||||
current_state = next_state
|
||||
self.adlist[current_state]["output"].append(keyword)
|
||||
|
||||
def set_fail_transitions(self) -> None:
|
||||
q = deque()
|
||||
q: deque = deque()
|
||||
for node in self.adlist[0]["next_states"]:
|
||||
q.append(node)
|
||||
self.adlist[node]["fail_state"] = 0
|
||||
|
@ -68,7 +69,9 @@ class Automaton:
|
|||
>>> A.search_in("whatever, err ... , wherever")
|
||||
{'what': [0], 'hat': [1], 'ver': [5, 25], 'er': [6, 10, 22, 26]}
|
||||
"""
|
||||
result = dict() # returns a dict with keywords and list of its occurrences
|
||||
result: dict = (
|
||||
dict()
|
||||
) # returns a dict with keywords and list of its occurrences
|
||||
current_state = 0
|
||||
for i in range(len(string)):
|
||||
while (
|
||||
|
@ -76,10 +79,11 @@ class Automaton:
|
|||
and current_state != 0
|
||||
):
|
||||
current_state = self.adlist[current_state]["fail_state"]
|
||||
current_state = self.find_next_state(current_state, string[i])
|
||||
if current_state is None:
|
||||
next_state = self.find_next_state(current_state, string[i])
|
||||
if next_state is None:
|
||||
current_state = 0
|
||||
else:
|
||||
current_state = next_state
|
||||
for key in self.adlist[current_state]["output"]:
|
||||
if not (key in result):
|
||||
result[key] = []
|
||||
|
|
|
@ -35,27 +35,28 @@ def palindromic_string(input_string: str) -> str:
|
|||
length = [1 for i in range(len(new_input_string))]
|
||||
|
||||
# for each character in new_string find corresponding palindromic string
|
||||
for i in range(len(new_input_string)):
|
||||
k = 1 if i > r else min(length[l + r - i] // 2, r - i + 1)
|
||||
start = 0
|
||||
for j in range(len(new_input_string)):
|
||||
k = 1 if j > r else min(length[l + r - j] // 2, r - j + 1)
|
||||
while (
|
||||
i - k >= 0
|
||||
and i + k < len(new_input_string)
|
||||
and new_input_string[k + i] == new_input_string[i - k]
|
||||
j - k >= 0
|
||||
and j + k < len(new_input_string)
|
||||
and new_input_string[k + j] == new_input_string[j - k]
|
||||
):
|
||||
k += 1
|
||||
|
||||
length[i] = 2 * k - 1
|
||||
length[j] = 2 * k - 1
|
||||
|
||||
# does this string is ending after the previously explored end (that is r) ?
|
||||
# if yes the update the new r to the last index of this
|
||||
if i + k - 1 > r:
|
||||
l = i - k + 1 # noqa: E741
|
||||
r = i + k - 1
|
||||
if j + k - 1 > r:
|
||||
l = j - k + 1 # noqa: E741
|
||||
r = j + k - 1
|
||||
|
||||
# update max_length and start position
|
||||
if max_length < length[i]:
|
||||
max_length = length[i]
|
||||
start = i
|
||||
if max_length < length[j]:
|
||||
max_length = length[j]
|
||||
start = j
|
||||
|
||||
# create that string
|
||||
s = new_input_string[start - max_length // 2 : start + max_length // 2 + 1]
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
from typing import List, Tuple
|
||||
|
||||
"""
|
||||
Algorithm for calculating the most cost-efficient sequence for converting one string
|
||||
into another.
|
||||
|
@ -18,7 +16,7 @@ def compute_transform_tables(
|
|||
replace_cost: int,
|
||||
delete_cost: int,
|
||||
insert_cost: int,
|
||||
) -> Tuple[List[int], List[str]]:
|
||||
) -> tuple[list[list[int]], list[list[str]]]:
|
||||
source_seq = list(source_string)
|
||||
destination_seq = list(destination_string)
|
||||
len_source_seq = len(source_seq)
|
||||
|
@ -28,7 +26,7 @@ def compute_transform_tables(
|
|||
[0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
|
||||
]
|
||||
ops = [
|
||||
[0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
|
||||
["0" for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
|
||||
]
|
||||
|
||||
for i in range(1, len_source_seq + 1):
|
||||
|
@ -59,7 +57,7 @@ def compute_transform_tables(
|
|||
return costs, ops
|
||||
|
||||
|
||||
def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]:
|
||||
def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
|
||||
if i == 0 and j == 0:
|
||||
return []
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue
Block a user