From a5794bef9e119184e6d7bf1f4de1af9864f744e3 Mon Sep 17 00:00:00 2001 From: Dmytro Litvinov Date: Tue, 6 Oct 2020 07:32:42 +0300 Subject: [PATCH 1/3] Add type hints for strings/ folder --- strings/aho_corasick.py | 11 ++++++----- strings/boyer_moore_search.py | 9 +++++---- strings/knuth_morris_pratt.py | 7 +++++-- strings/levenshtein_distance.py | 2 +- strings/manacher.py | 2 +- strings/rabin_karp.py | 4 ++-- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/strings/aho_corasick.py b/strings/aho_corasick.py index bb6955bdd423..b959dbd58c32 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 @@ 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): @@ -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) @@ -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") diff --git a/strings/boyer_moore_search.py b/strings/boyer_moore_search.py index 9d32a6943906..a3e6cf614eab 100644 --- a/strings/boyer_moore_search.py +++ b/strings/boyer_moore_search.py @@ -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 : @@ -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 @@ -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): diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 2e5e0c7e73f1..a205ce37e3e5 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 9b8793544a99..54948a96670b 100644 --- a/strings/levenshtein_distance.py +++ b/strings/levenshtein_distance.py @@ -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. diff --git a/strings/manacher.py b/strings/manacher.py index 73b31a7bea9f..5476e06839b7 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 d866b1397277..81ca611a76b3 100644 --- a/strings/rabin_karp.py +++ b/strings/rabin_karp.py @@ -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 @@ -51,7 +51,7 @@ def rabin_karp(pattern, text): return False -def test_rabin_karp(): +def test_rabin_karp() -> None: """ >>> test_rabin_karp() Success. From 92a5b7a1d7c98b9ae3dca87c63c92085775a8266 Mon Sep 17 00:00:00 2001 From: Dmytro Litvinov Date: Tue, 6 Oct 2020 11:15:51 +0300 Subject: [PATCH 2/3] Rerun other checks From 368e735bd50bb43385e067043d823ced5e7e56c9 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 6 Oct 2020 08:17:11 +0000 Subject: [PATCH 3/3] updating DIRECTORY.md --- DIRECTORY.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index a8d00f6cb724..6a3d31709ed6 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)