Skip to content

Commit a5794be

Browse files
author
Dmytro Litvinov
committed
Add type hints for strings/ folder
1 parent f36a2f6 commit a5794be

6 files changed

+20
-15
lines changed

strings/aho_corasick.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from collections import deque
2+
from typing import Dict, List, Union
23

34

45
class Automaton:
5-
def __init__(self, keywords):
6+
def __init__(self, keywords: List[str]):
67
self.adlist = list()
78
self.adlist.append(
89
{"value": "", "next_states": [], "fail_state": 0, "output": []}
@@ -12,13 +13,13 @@ def __init__(self, keywords):
1213
self.add_keyword(keyword)
1314
self.set_fail_transitions()
1415

15-
def find_next_state(self, current_state, char):
16+
def find_next_state(self, current_state: int, char: str) -> Union[int, None]:
1617
for state in self.adlist[current_state]["next_states"]:
1718
if char == self.adlist[state]["value"]:
1819
return state
1920
return None
2021

21-
def add_keyword(self, keyword):
22+
def add_keyword(self, keyword: str) -> None:
2223
current_state = 0
2324
for character in keyword:
2425
if self.find_next_state(current_state, character):
@@ -36,7 +37,7 @@ def add_keyword(self, keyword):
3637
current_state = len(self.adlist) - 1
3738
self.adlist[current_state]["output"].append(keyword)
3839

39-
def set_fail_transitions(self):
40+
def set_fail_transitions(self) -> None:
4041
q = deque()
4142
for node in self.adlist[0]["next_states"]:
4243
q.append(node)
@@ -61,7 +62,7 @@ def set_fail_transitions(self):
6162
+ self.adlist[self.adlist[child]["fail_state"]]["output"]
6263
)
6364

64-
def search_in(self, string):
65+
def search_in(self, string: str) -> Dict[str, List[int]]:
6566
"""
6667
>>> A = Automaton(["what", "hat", "ver", "er"])
6768
>>> A.search_in("whatever, err ... , wherever")

strings/boyer_moore_search.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
n=length of main string
1818
m=length of pattern string
1919
"""
20+
from typing import List
2021

2122

2223
class BoyerMooreSearch:
23-
def __init__(self, text, pattern):
24+
def __init__(self, text: str, pattern: str):
2425
self.text, self.pattern = text, pattern
2526
self.textLen, self.patLen = len(text), len(pattern)
2627

27-
def match_in_pattern(self, char):
28+
def match_in_pattern(self, char: str) -> int:
2829
"""finds the index of char in pattern in reverse order
2930
3031
Parameters :
@@ -40,7 +41,7 @@ def match_in_pattern(self, char):
4041
return i
4142
return -1
4243

43-
def mismatch_in_text(self, currentPos):
44+
def mismatch_in_text(self, currentPos: int) -> int:
4445
"""
4546
find the index of mis-matched character in text when compared with pattern
4647
from last
@@ -58,7 +59,7 @@ def mismatch_in_text(self, currentPos):
5859
return currentPos + i
5960
return -1
6061

61-
def bad_character_heuristic(self):
62+
def bad_character_heuristic(self) -> List[int]:
6263
# searches pattern in text and returns index positions
6364
positions = []
6465
for i in range(self.textLen - self.patLen + 1):

strings/knuth_morris_pratt.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
def kmp(pattern, text):
1+
from typing import List
2+
3+
4+
def kmp(pattern: str, text: str) -> bool:
25
"""
36
The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text
47
with complexity O(n + m)
@@ -33,7 +36,7 @@ def kmp(pattern, text):
3336
return False
3437

3538

36-
def get_failure_array(pattern):
39+
def get_failure_array(pattern: str) -> List[int]:
3740
"""
3841
Calculates the new index we should go to if we fail a comparison
3942
:param pattern:

strings/levenshtein_distance.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"""
1414

1515

16-
def levenshtein_distance(first_word, second_word):
16+
def levenshtein_distance(first_word: str, second_word: str) -> int:
1717
"""Implementation of the levenshtein distance in Python.
1818
:param first_word: the first word to measure the difference.
1919
:param second_word: the second word to measure the difference.

strings/manacher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def palindromic_string(input_string):
1+
def palindromic_string(input_string: str) -> str:
22
"""
33
>>> palindromic_string('abbbaba')
44
'abbba'

strings/rabin_karp.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
modulus = 1000003
55

66

7-
def rabin_karp(pattern, text):
7+
def rabin_karp(pattern: str, text: str) -> bool:
88
"""
99
The Rabin-Karp Algorithm for finding a pattern within a piece of text
1010
with complexity O(nm), most efficient when it is used with multiple patterns
@@ -51,7 +51,7 @@ def rabin_karp(pattern, text):
5151
return False
5252

5353

54-
def test_rabin_karp():
54+
def test_rabin_karp() -> None:
5555
"""
5656
>>> test_rabin_karp()
5757
Success.

0 commit comments

Comments
 (0)