mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
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:
parent
f36a2f621e
commit
000cedc07f
|
@ -552,6 +552,8 @@
|
||||||
* Problem 12
|
* Problem 12
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol1.py)
|
* [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)
|
* [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
|
* Problem 13
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_13/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_13/sol1.py)
|
||||||
* Problem 14
|
* Problem 14
|
||||||
|
@ -597,7 +599,7 @@
|
||||||
* Problem 29
|
* Problem 29
|
||||||
* [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/solution.py)
|
* [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/solution.py)
|
||||||
* Problem 30
|
* 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
|
* Problem 31
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol1.py)
|
* [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)
|
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol2.py)
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
from collections import deque
|
from collections import deque
|
||||||
|
from typing import Dict, List, Union
|
||||||
|
|
||||||
|
|
||||||
class Automaton:
|
class Automaton:
|
||||||
def __init__(self, keywords):
|
def __init__(self, keywords: List[str]):
|
||||||
self.adlist = list()
|
self.adlist = list()
|
||||||
self.adlist.append(
|
self.adlist.append(
|
||||||
{"value": "", "next_states": [], "fail_state": 0, "output": []}
|
{"value": "", "next_states": [], "fail_state": 0, "output": []}
|
||||||
|
@ -12,13 +13,13 @@ class Automaton:
|
||||||
self.add_keyword(keyword)
|
self.add_keyword(keyword)
|
||||||
self.set_fail_transitions()
|
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"]:
|
for state in self.adlist[current_state]["next_states"]:
|
||||||
if char == self.adlist[state]["value"]:
|
if char == self.adlist[state]["value"]:
|
||||||
return state
|
return state
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def add_keyword(self, keyword):
|
def add_keyword(self, keyword: str) -> None:
|
||||||
current_state = 0
|
current_state = 0
|
||||||
for character in keyword:
|
for character in keyword:
|
||||||
if self.find_next_state(current_state, character):
|
if self.find_next_state(current_state, character):
|
||||||
|
@ -36,7 +37,7 @@ class Automaton:
|
||||||
current_state = len(self.adlist) - 1
|
current_state = len(self.adlist) - 1
|
||||||
self.adlist[current_state]["output"].append(keyword)
|
self.adlist[current_state]["output"].append(keyword)
|
||||||
|
|
||||||
def set_fail_transitions(self):
|
def set_fail_transitions(self) -> None:
|
||||||
q = deque()
|
q = deque()
|
||||||
for node in self.adlist[0]["next_states"]:
|
for node in self.adlist[0]["next_states"]:
|
||||||
q.append(node)
|
q.append(node)
|
||||||
|
@ -61,7 +62,7 @@ class Automaton:
|
||||||
+ self.adlist[self.adlist[child]["fail_state"]]["output"]
|
+ 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 = Automaton(["what", "hat", "ver", "er"])
|
||||||
>>> A.search_in("whatever, err ... , wherever")
|
>>> A.search_in("whatever, err ... , wherever")
|
||||||
|
|
|
@ -17,14 +17,15 @@ Time Complexity : O(n/m)
|
||||||
n=length of main string
|
n=length of main string
|
||||||
m=length of pattern string
|
m=length of pattern string
|
||||||
"""
|
"""
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
class BoyerMooreSearch:
|
class BoyerMooreSearch:
|
||||||
def __init__(self, text, pattern):
|
def __init__(self, text: str, pattern: str):
|
||||||
self.text, self.pattern = text, pattern
|
self.text, self.pattern = text, pattern
|
||||||
self.textLen, self.patLen = len(text), len(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
|
"""finds the index of char in pattern in reverse order
|
||||||
|
|
||||||
Parameters :
|
Parameters :
|
||||||
|
@ -40,7 +41,7 @@ class BoyerMooreSearch:
|
||||||
return i
|
return i
|
||||||
return -1
|
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
|
find the index of mis-matched character in text when compared with pattern
|
||||||
from last
|
from last
|
||||||
|
@ -58,7 +59,7 @@ class BoyerMooreSearch:
|
||||||
return currentPos + i
|
return currentPos + i
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
def bad_character_heuristic(self):
|
def bad_character_heuristic(self) -> List[int]:
|
||||||
# searches pattern in text and returns index positions
|
# searches pattern in text and returns index positions
|
||||||
positions = []
|
positions = []
|
||||||
for i in range(self.textLen - self.patLen + 1):
|
for i in range(self.textLen - self.patLen + 1):
|
||||||
|
|
|
@ -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
|
The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text
|
||||||
with complexity O(n + m)
|
with complexity O(n + m)
|
||||||
|
@ -33,7 +36,7 @@ def kmp(pattern, text):
|
||||||
return False
|
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
|
Calculates the new index we should go to if we fail a comparison
|
||||||
:param pattern:
|
:param pattern:
|
||||||
|
|
|
@ -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.
|
"""Implementation of the levenshtein distance in Python.
|
||||||
:param first_word: the first word to measure the difference.
|
:param first_word: the first word to measure the difference.
|
||||||
:param second_word: the second word to measure the difference.
|
:param second_word: the second word to measure the difference.
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
def palindromic_string(input_string):
|
def palindromic_string(input_string: str) -> str:
|
||||||
"""
|
"""
|
||||||
>>> palindromic_string('abbbaba')
|
>>> palindromic_string('abbbaba')
|
||||||
'abbba'
|
'abbba'
|
||||||
|
|
|
@ -4,7 +4,7 @@ alphabet_size = 256
|
||||||
modulus = 1000003
|
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
|
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
|
with complexity O(nm), most efficient when it is used with multiple patterns
|
||||||
|
@ -51,7 +51,7 @@ def rabin_karp(pattern, text):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def test_rabin_karp():
|
def test_rabin_karp() -> None:
|
||||||
"""
|
"""
|
||||||
>>> test_rabin_karp()
|
>>> test_rabin_karp()
|
||||||
Success.
|
Success.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user