Skip to content

[mypy] Fix type annotations for strings #4641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions strings/aho_corasick.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


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": []}
)
Expand All @@ -22,9 +22,8 @@ def find_next_state(self, current_state: int, char: str) -> Union[int, None]:
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,
Expand All @@ -35,10 +34,12 @@ def add_keyword(self, keyword: str) -> None:
)
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
Expand Down Expand Up @@ -68,18 +69,21 @@ def search_in(self, string: str) -> Dict[str, List[int]]:
>>> 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 (
self.find_next_state(current_state, string[i]) is None
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] = []
Expand Down
25 changes: 13 additions & 12 deletions strings/manacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 3 additions & 5 deletions strings/min_cost_string_conversion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import List, Tuple

"""
Algorithm for calculating the most cost-efficient sequence for converting one string
into another.
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down