Add type hints for "strings" folder (#2882)

* Add type hints for strings/ folder

* Rerun other checks

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Dmytro Litvinov 2020-10-06 11:31:15 +03:00 committed by GitHub
parent f36a2f621e
commit 000cedc07f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 16 deletions

View File

@ -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
@ -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)

View File

@ -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": []}
@ -12,13 +13,13 @@ class Automaton:
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):
@ -36,7 +37,7 @@ class Automaton:
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)
@ -61,7 +62,7 @@ class Automaton:
+ 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")

View File

@ -17,14 +17,15 @@ Time Complexity : O(n/m)
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 :
@ -40,7 +41,7 @@ class BoyerMooreSearch:
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
@ -58,7 +59,7 @@ class BoyerMooreSearch:
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):

View File

@ -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)
@ -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:

View File

@ -13,7 +13,7 @@ python levenshtein-distance.py
"""
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.

View File

@ -1,4 +1,4 @@
def palindromic_string(input_string):
def palindromic_string(input_string: str) -> str:
"""
>>> palindromic_string('abbbaba')
'abbba'

View File

@ -4,7 +4,7 @@ alphabet_size = 256
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
@ -51,7 +51,7 @@ def rabin_karp(pattern, text):
return False
def test_rabin_karp():
def test_rabin_karp() -> None:
"""
>>> test_rabin_karp()
Success.