diff --git a/DIRECTORY.md b/DIRECTORY.md index a8d00f6cb..6a3d31709 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/strings/aho_corasick.py b/strings/aho_corasick.py index bb6955bdd..b959dbd58 100644 --- a/strings/aho_corasick.py +++ b/strings/aho_corasick.py @@ -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") diff --git a/strings/boyer_moore_search.py b/strings/boyer_moore_search.py index 9d32a6943..a3e6cf614 100644 --- a/strings/boyer_moore_search.py +++ b/strings/boyer_moore_search.py @@ -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): diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 2e5e0c7e7..a205ce37e 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -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: diff --git a/strings/levenshtein_distance.py b/strings/levenshtein_distance.py index 9b8793544..54948a966 100644 --- a/strings/levenshtein_distance.py +++ b/strings/levenshtein_distance.py @@ -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. diff --git a/strings/manacher.py b/strings/manacher.py index 73b31a7be..5476e0683 100644 --- a/strings/manacher.py +++ b/strings/manacher.py @@ -1,4 +1,4 @@ -def palindromic_string(input_string): +def palindromic_string(input_string: str) -> str: """ >>> palindromic_string('abbbaba') 'abbba' diff --git a/strings/rabin_karp.py b/strings/rabin_karp.py index d866b1397..81ca611a7 100644 --- a/strings/rabin_karp.py +++ b/strings/rabin_karp.py @@ -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.