Skip to content

Add type hints for "strings" folder #2882

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
Oct 6, 2020
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
4 changes: 3 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@
* Problem 12
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol1.py)
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol2.py)
* Problem 120
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_120/sol1.py)
* Problem 13
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_13/sol1.py)
* Problem 14
Expand Down Expand Up @@ -597,7 +599,7 @@
* Problem 29
* [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/solution.py)
* Problem 30
* [Soln](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_30/soln.py)
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_30/sol1.py)
* Problem 31
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol1.py)
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol2.py)
Expand Down
11 changes: 6 additions & 5 deletions strings/aho_corasick.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from collections import deque
from typing import Dict, List, Union


class Automaton:
def __init__(self, keywords):
def __init__(self, keywords: List[str]):
self.adlist = list()
self.adlist.append(
{"value": "", "next_states": [], "fail_state": 0, "output": []}
Expand All @@ -12,13 +13,13 @@ def __init__(self, keywords):
self.add_keyword(keyword)
self.set_fail_transitions()

def find_next_state(self, current_state, char):
def find_next_state(self, current_state: int, char: str) -> Union[int, None]:
for state in self.adlist[current_state]["next_states"]:
if char == self.adlist[state]["value"]:
return state
return None

def add_keyword(self, keyword):
def add_keyword(self, keyword: str) -> None:
current_state = 0
for character in keyword:
if self.find_next_state(current_state, character):
Expand All @@ -36,7 +37,7 @@ def add_keyword(self, keyword):
current_state = len(self.adlist) - 1
self.adlist[current_state]["output"].append(keyword)

def set_fail_transitions(self):
def set_fail_transitions(self) -> None:
q = deque()
for node in self.adlist[0]["next_states"]:
q.append(node)
Expand All @@ -61,7 +62,7 @@ def set_fail_transitions(self):
+ self.adlist[self.adlist[child]["fail_state"]]["output"]
)

def search_in(self, string):
def search_in(self, string: str) -> Dict[str, List[int]]:
"""
>>> A = Automaton(["what", "hat", "ver", "er"])
>>> A.search_in("whatever, err ... , wherever")
Expand Down
9 changes: 5 additions & 4 deletions strings/boyer_moore_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
n=length of main string
m=length of pattern string
"""
from typing import List


class BoyerMooreSearch:
def __init__(self, text, pattern):
def __init__(self, text: str, pattern: str):
self.text, self.pattern = text, pattern
self.textLen, self.patLen = len(text), len(pattern)

def match_in_pattern(self, char):
def match_in_pattern(self, char: str) -> int:
"""finds the index of char in pattern in reverse order

Parameters :
Expand All @@ -40,7 +41,7 @@ def match_in_pattern(self, char):
return i
return -1

def mismatch_in_text(self, currentPos):
def mismatch_in_text(self, currentPos: int) -> int:
"""
find the index of mis-matched character in text when compared with pattern
from last
Expand All @@ -58,7 +59,7 @@ def mismatch_in_text(self, currentPos):
return currentPos + i
return -1

def bad_character_heuristic(self):
def bad_character_heuristic(self) -> List[int]:
# searches pattern in text and returns index positions
positions = []
for i in range(self.textLen - self.patLen + 1):
Expand Down
7 changes: 5 additions & 2 deletions strings/knuth_morris_pratt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
def kmp(pattern, text):
from typing import List


def kmp(pattern: str, text: str) -> bool:
"""
The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text
with complexity O(n + m)
Expand Down Expand Up @@ -33,7 +36,7 @@ def kmp(pattern, text):
return False


def get_failure_array(pattern):
def get_failure_array(pattern: str) -> List[int]:
"""
Calculates the new index we should go to if we fail a comparison
:param pattern:
Expand Down
2 changes: 1 addition & 1 deletion strings/levenshtein_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""


def levenshtein_distance(first_word, second_word):
def levenshtein_distance(first_word: str, second_word: str) -> int:
"""Implementation of the levenshtein distance in Python.
:param first_word: the first word to measure the difference.
:param second_word: the second word to measure the difference.
Expand Down
2 changes: 1 addition & 1 deletion strings/manacher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def palindromic_string(input_string):
def palindromic_string(input_string: str) -> str:
"""
>>> palindromic_string('abbbaba')
'abbba'
Expand Down
4 changes: 2 additions & 2 deletions strings/rabin_karp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
modulus = 1000003


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


def test_rabin_karp():
def test_rabin_karp() -> None:
"""
>>> test_rabin_karp()
Success.
Expand Down