Skip to content

improve maintainability index mi 55.888 -> 91.1844 #2783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'master' into 2128#issuecomment-703119780-maintainability
  • Loading branch information
ronnydw authored Oct 7, 2020
commit 74b20d5d0173bf3266ea318a1aaee999c770c278
114 changes: 57 additions & 57 deletions strings/manacher.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
import unittest
def longest_palindromic_substring(string: str) -> str:
"""
Manacher’s algorithm which finds Longest Palindromic Substring.
Source: https://en.wikipedia.org/wiki/Longest_palindromic_substring
"""
if string == "":
return ""
# create string with separators: "aba" -> "a|b|a"
new_string = "|".join(list(string))
left = right = 0 # start/end index of longest palindrome so far
# length[i] shows the length of palindromic substring with center i
length = [1] * len(new_string)
start = max_length = 0
# for each character in new_string find the corresponding max palindrome length
# and store the length and left, right boundary
for i, _ in enumerate(new_string):
k = 1 if i > right else min(length[left + right - i] // 2, right - i + 1)
while (
i - k >= 0
and i + k < len(new_string)
and new_string[k + i] == new_string[i - k]
):
k += 1
length[i] = 2 * k - 1
# update the right and left index if string ends after the previously right
if i + k - 1 > right:
left = i - k + 1 # noqa: E741
right = i + k - 1
if max_length < length[i]:
max_length = length[i]
start = i
s = new_string[start - max_length // 2 : start + max_length // 2 + 1]
return "".join(s.split("|"))
class TestLongestPalindrome(unittest.TestCase):
def test_longest_palindromic_substring(self):
self.assertEqual(longest_palindromic_substring("abbbaba"), "abbba")
self.assertEqual(longest_palindromic_substring("ababa"), "ababa")
self.assertEqual(longest_palindromic_substring(""), "")
self.assertEqual(longest_palindromic_substring("a"), "a")
self.assertEqual(longest_palindromic_substring("aa"), "aa")
self.assertEqual(longest_palindromic_substring("abc"), "b")
if __name__ == "__main__":
unittest.main()
import unittest


def longest_palindromic_substring(string: str) -> str:
"""
Manacher’s algorithm which finds Longest Palindromic Substring.
Source: https://en.wikipedia.org/wiki/Longest_palindromic_substring
"""
if string == "":
return ""

# create string with separators: "aba" -> "a|b|a"
new_string = "|".join(list(string))

left = right = 0 # start/end index of longest palindrome so far
# length[i] shows the length of palindromic substring with center i
length = [1] * len(new_string)
start = max_length = 0

# for each character in new_string find the corresponding max palindrome length
# and store the length and left, right boundary
for i, _ in enumerate(new_string):
k = 1 if i > right else min(length[left + right - i] // 2, right - i + 1)
while (
i - k >= 0
and i + k < len(new_string)
and new_string[k + i] == new_string[i - k]
):
k += 1

length[i] = 2 * k - 1

# update the right and left index if string ends after the previously right
if i + k - 1 > right:
left = i - k + 1 # noqa: E741
right = i + k - 1

if max_length < length[i]:
max_length = length[i]
start = i

s = new_string[start - max_length // 2 : start + max_length // 2 + 1]
return "".join(s.split("|"))


class TestLongestPalindrome(unittest.TestCase):
def test_longest_palindromic_substring(self):
self.assertEqual(longest_palindromic_substring("abbbaba"), "abbba")
self.assertEqual(longest_palindromic_substring("ababa"), "ababa")
self.assertEqual(longest_palindromic_substring(""), "")
self.assertEqual(longest_palindromic_substring("a"), "a")
self.assertEqual(longest_palindromic_substring("aa"), "aa")
self.assertEqual(longest_palindromic_substring("abc"), "b")


if __name__ == "__main__":
unittest.main()
You are viewing a condensed version of this merge commit. You can view the full changes here.