mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Tighten up psf/black and flake8 (#2024)
* Tighten up psf/black and flake8
* Fix some tests
* Fix some E741
* Fix some E741
* updating DIRECTORY.md
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
"""
|
||||
The algorithm finds the pattern in given text using following rule.
|
||||
|
||||
The bad-character rule considers the mismatched character in Text.
|
||||
The next occurrence of that character to the left in Pattern is found,
|
||||
The bad-character rule considers the mismatched character in Text.
|
||||
The next occurrence of that character to the left in Pattern is found,
|
||||
|
||||
If the mismatched character occurs to the left in Pattern,
|
||||
a shift is proposed that aligns text block and pattern.
|
||||
If the mismatched character occurs to the left in Pattern,
|
||||
a shift is proposed that aligns text block and pattern.
|
||||
|
||||
If the mismatched character does not occur to the left in Pattern,
|
||||
a shift is proposed that moves the entirety of Pattern past
|
||||
the point of mismatch in the text.
|
||||
If the mismatched character does not occur to the left in Pattern,
|
||||
a shift is proposed that moves the entirety of Pattern past
|
||||
the point of mismatch in the text.
|
||||
|
||||
If there no mismatch then the pattern matches with text block.
|
||||
|
||||
@@ -27,12 +27,12 @@ class BoyerMooreSearch:
|
||||
def match_in_pattern(self, char):
|
||||
""" finds the index of char in pattern in reverse order
|
||||
|
||||
Parameters :
|
||||
Parameters :
|
||||
char (chr): character to be searched
|
||||
|
||||
|
||||
Returns :
|
||||
i (int): index of char from last in pattern
|
||||
-1 (int): if char is not found in pattern
|
||||
-1 (int): if char is not found in pattern
|
||||
"""
|
||||
|
||||
for i in range(self.patLen - 1, -1, -1):
|
||||
@@ -43,9 +43,9 @@ class BoyerMooreSearch:
|
||||
def mismatch_in_text(self, currentPos):
|
||||
""" finds the index of mis-matched character in text when compared with pattern from last
|
||||
|
||||
Parameters :
|
||||
Parameters :
|
||||
currentPos (int): current index position of text
|
||||
|
||||
|
||||
Returns :
|
||||
i (int): index of mismatched char from last in text
|
||||
-1 (int): if there is no mismatch between pattern and text block
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
def lower(word: str) -> str:
|
||||
|
||||
"""
|
||||
Will convert the entire string to lowecase letters
|
||||
|
||||
"""
|
||||
Will convert the entire string to lowecase letters
|
||||
|
||||
>>> lower("wow")
|
||||
'wow'
|
||||
>>> lower("HellZo")
|
||||
'hellzo'
|
||||
>>> lower("WHAT")
|
||||
'what'
|
||||
|
||||
|
||||
>>> lower("wh[]32")
|
||||
'wh[]32'
|
||||
>>> lower("whAT")
|
||||
|
||||
@@ -9,9 +9,10 @@ def palindromic_string(input_string):
|
||||
|
||||
1. first this convert input_string("xyx") into new_string("x|y|x") where odd
|
||||
positions are actual input characters.
|
||||
2. for each character in new_string it find corresponding length and store the length
|
||||
and l,r to store previously calculated info.(please look the explanation for details)
|
||||
|
||||
2. for each character in new_string it find corresponding length and store the
|
||||
length and l,r to store previously calculated info.(please look the explanation
|
||||
for details)
|
||||
|
||||
3. return corresponding output_string by removing all "|"
|
||||
"""
|
||||
max_length = 0
|
||||
@@ -26,7 +27,8 @@ def palindromic_string(input_string):
|
||||
# append last character
|
||||
new_input_string += input_string[-1]
|
||||
|
||||
# we will store the starting and ending of previous furthest ending palindromic substring
|
||||
# we will store the starting and ending of previous furthest ending palindromic
|
||||
# substring
|
||||
l, r = 0, 0
|
||||
|
||||
# length[i] shows the length of palindromic substring with center i
|
||||
@@ -47,7 +49,7 @@ def palindromic_string(input_string):
|
||||
# does this string is ending after the previously explored end (that is r) ?
|
||||
# if yes the update the new r to the last index of this
|
||||
if i + k - 1 > r:
|
||||
l = i - k + 1
|
||||
l = i - k + 1 # noqa: E741
|
||||
r = i + k - 1
|
||||
|
||||
# update max_length and start position
|
||||
@@ -72,32 +74,34 @@ if __name__ == "__main__":
|
||||
"""
|
||||
...a0...a1...a2.....a3......a4...a5...a6....
|
||||
|
||||
consider the string for which we are calculating the longest palindromic substring is shown above where ...
|
||||
are some characters in between and right now we are calculating the length of palindromic substring with
|
||||
center at a5 with following conditions :
|
||||
i) we have stored the length of palindromic substring which has center at a3 (starts at l ends at r) and it
|
||||
is the furthest ending till now, and it has ending after a6
|
||||
consider the string for which we are calculating the longest palindromic substring is
|
||||
shown above where ... are some characters in between and right now we are calculating
|
||||
the length of palindromic substring with center at a5 with following conditions :
|
||||
i) we have stored the length of palindromic substring which has center at a3 (starts at
|
||||
l ends at r) and it is the furthest ending till now, and it has ending after a6
|
||||
ii) a2 and a4 are equally distant from a3 so char(a2) == char(a4)
|
||||
iii) a0 and a6 are equally distant from a3 so char(a0) == char(a6)
|
||||
iv) a1 is corresponding equal character of a5 in palindrome with center a3 (remember that in below derivation of a4==a6)
|
||||
iv) a1 is corresponding equal character of a5 in palindrome with center a3 (remember
|
||||
that in below derivation of a4==a6)
|
||||
|
||||
now for a5 we will calculate the length of palindromic substring with center as a5 but can we use previously
|
||||
calculated information in some way?
|
||||
Yes, look the above string we know that a5 is inside the palindrome with center a3 and previously we have
|
||||
have calculated that
|
||||
now for a5 we will calculate the length of palindromic substring with center as a5 but
|
||||
can we use previously calculated information in some way?
|
||||
Yes, look the above string we know that a5 is inside the palindrome with center a3 and
|
||||
previously we have have calculated that
|
||||
a0==a2 (palindrome of center a1)
|
||||
a2==a4 (palindrome of center a3)
|
||||
a0==a6 (palindrome of center a3)
|
||||
so a4==a6
|
||||
|
||||
so we can say that palindrome at center a5 is at least as long as palindrome at center a1
|
||||
but this only holds if a0 and a6 are inside the limits of palindrome centered at a3 so finally ..
|
||||
so we can say that palindrome at center a5 is at least as long as palindrome at center
|
||||
a1 but this only holds if a0 and a6 are inside the limits of palindrome centered at a3
|
||||
so finally ..
|
||||
|
||||
len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), r-a5)
|
||||
where a3 lies from l to r and we have to keep updating that
|
||||
|
||||
and if the a5 lies outside of l,r boundary we calculate length of palindrome with bruteforce and update
|
||||
l,r.
|
||||
and if the a5 lies outside of l,r boundary we calculate length of palindrome with
|
||||
bruteforce and update l,r.
|
||||
|
||||
it gives the linear time complexity just like z-function
|
||||
"""
|
||||
|
||||
@@ -11,10 +11,7 @@ def reverse_words(input_str: str) -> str:
|
||||
>>> reverse_words(sentence)
|
||||
'Python love I'
|
||||
"""
|
||||
input_str = input_str.split(" ")
|
||||
new_str = list()
|
||||
|
||||
return " ".join(reversed(input_str))
|
||||
return " ".join(reversed(input_str.split(" ")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
def split(string: str, separator: str = " ") -> list:
|
||||
"""
|
||||
Will split the string up into all the values separated by the separator (defaults to spaces)
|
||||
|
||||
|
||||
>>> split("apple#banana#cherry#orange",separator='#')
|
||||
['apple', 'banana', 'cherry', 'orange']
|
||||
|
||||
|
||||
>>> split("Hello there")
|
||||
['Hello', 'there']
|
||||
|
||||
|
||||
>>> split("11/22/63",separator = '/')
|
||||
['11', '22', '63']
|
||||
|
||||
|
||||
>>> split("12:43:39",separator = ":")
|
||||
['12', '43', '39']
|
||||
"""
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
def upper(word: str) -> str:
|
||||
"""
|
||||
Will convert the entire string to uppercase letters
|
||||
|
||||
"""
|
||||
Will convert the entire string to uppercase letters
|
||||
|
||||
>>> upper("wow")
|
||||
'WOW'
|
||||
>>> upper("Hello")
|
||||
'HELLO'
|
||||
>>> upper("WHAT")
|
||||
'WHAT'
|
||||
|
||||
|
||||
>>> upper("wh[]32")
|
||||
'WH[]32'
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user