From b4591cf2223a8dda0cdc50a97fdb4498adca794d Mon Sep 17 00:00:00 2001 From: "MD. FARHAN LABIB JAHIN" <68538382+Farhan-labib@users.noreply.github.com> Date: Mon, 9 Sep 2024 00:36:12 +0600 Subject: [PATCH 1/4] Create Longest Palindrome --- strings/Longest Palindrome | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 strings/Longest Palindrome diff --git a/strings/Longest Palindrome b/strings/Longest Palindrome new file mode 100644 index 000000000000..5613aaa69a36 --- /dev/null +++ b/strings/Longest Palindrome @@ -0,0 +1,37 @@ +def longest_palindrome(s: str) -> str: + if s == s[::-1] or len(s) == 1: + return s + else: + res = [s[i:j] for i in range(len(s)) for j in range(i + 1, len(s) + 1)] + output = "" + for k in res: + if k == k[::-1]: + if len(k) > len(output) or (len(k) == len(output) and res.index(k) < res.index(output)): + output = k + return output + + +# test cases with longer strings containing multiple palindromes +test_data = { + "abacdfgdcaba": "abacdcbadca", # Multiple palindromes, longest is "abacdcbadca" + "abcdcbacdcba": "abcdcbacdcba", # The entire string is a palindrome + "aabbcbbaaaab": "aabbcbbaaaab", # The entire string is a palindrome + "racecarlevelcivic": "racecar", # "racecar" is the longest palindrome + "madamimadam": "madamimadam", # The entire string is a palindrome + "abccbaabcdcb": "abccba", # "abccba" is the longest palindrome + "noonracecar": "racecar", # "racecar" is the longest palindrome + "aabbcccbbbaa": "bbb", # "bbb" is the longest palindrome + "detartrated": "detartrated", # The entire string is a palindrome + "civicradarlevel": "civic" # "civic" is the longest palindrome +} + + +# Main code block +if __name__ == "__main__": + for key, value in test_data.items(): + result = longest_palindrome(key) + print(f"{key:<35} -> Longest Palindrome: {result}") + assert result == value, f"Test failed for {key}. Expected {value}, but got {result}" + + # If all tests pass + print("All tests passed!") From 57a2009d7a5932e3b6dfe38b14e1512f53c8082d Mon Sep 17 00:00:00 2001 From: "MD. FARHAN LABIB JAHIN" <68538382+Farhan-labib@users.noreply.github.com> Date: Mon, 9 Sep 2024 00:39:09 +0600 Subject: [PATCH 2/4] Rename Longest Palindrome to longest_palindrome --- strings/{Longest Palindrome => longest_palindrome} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename strings/{Longest Palindrome => longest_palindrome} (100%) diff --git a/strings/Longest Palindrome b/strings/longest_palindrome similarity index 100% rename from strings/Longest Palindrome rename to strings/longest_palindrome From 1e30f5c633e7a7d1fa6c6ff16a6e221986084f59 Mon Sep 17 00:00:00 2001 From: "MD. FARHAN LABIB JAHIN" <68538382+Farhan-labib@users.noreply.github.com> Date: Wed, 11 Sep 2024 23:49:33 +0600 Subject: [PATCH 3/4] Update longest_palindrome --- strings/longest_palindrome | 55 ++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/strings/longest_palindrome b/strings/longest_palindrome index 5613aaa69a36..59d7dac22505 100644 --- a/strings/longest_palindrome +++ b/strings/longest_palindrome @@ -1,3 +1,5 @@ +import timeit + def longest_palindrome(s: str) -> str: if s == s[::-1] or len(s) == 1: return s @@ -11,27 +13,52 @@ def longest_palindrome(s: str) -> str: return output -# test cases with longer strings containing multiple palindromes +# Test cases with longer strings containing multiple palindromes test_data = { - "abacdfgdcaba": "abacdcbadca", # Multiple palindromes, longest is "abacdcbadca" - "abcdcbacdcba": "abcdcbacdcba", # The entire string is a palindrome - "aabbcbbaaaab": "aabbcbbaaaab", # The entire string is a palindrome - "racecarlevelcivic": "racecar", # "racecar" is the longest palindrome - "madamimadam": "madamimadam", # The entire string is a palindrome - "abccbaabcdcb": "abccba", # "abccba" is the longest palindrome - "noonracecar": "racecar", # "racecar" is the longest palindrome - "aabbcccbbbaa": "bbb", # "bbb" is the longest palindrome - "detartrated": "detartrated", # The entire string is a palindrome - "civicradarlevel": "civic" # "civic" is the longest palindrome + "abacdfgdcaba": "aba", + "abcdcbacdcba": "abcdcba", + "aabbcbbaaaab": "aabbcbbaa", + "racecarlevelcivic": "racecar", + "madamimadam": "madamimadam", + "abccbaabcdcb": "abccba", + "noonracecar": "racecar", + "aabbcccbbbaa": "bbcccbb", + "detartrated": "detartrated", + "civicradarlevel": "civic", + + # Additional complex cases + "babad": "bab", + "cbbd": "bb", + "forgeeksskeegfor": "geeksskeeg", + "abacabadabacaba": "abacabadabacaba", + "a": "a", + "bb": "bb", + "pqrzzzzzyxwvutsrqponmlkjihgfedcba": "zzzzz", + "abcdefg": "a", + "noonracecarlevel": "racecar", + "abcdedcbaefg": "abcdedcba", + "abacabad": "abacaba", + "aaaaaaa": "aaaaaaa", + + # Additional large test cases + "a" * 1000: "a" * 1000, } -# Main code block -if __name__ == "__main__": +def benchmark(): for key, value in test_data.items(): + start_time = timeit.default_timer() result = longest_palindrome(key) - print(f"{key:<35} -> Longest Palindrome: {result}") + end_time = timeit.default_timer() + elapsed_time = end_time - start_time + + print(f"{key[:30]:<35} -> Longest Palindrome: {result} (Time: {elapsed_time:.6f} seconds)") assert result == value, f"Test failed for {key}. Expected {value}, but got {result}" # If all tests pass print("All tests passed!") + + +# Main code block +if __name__ == "__main__": + benchmark() From 2aa5a0e3851ee825d9dd3efcac46850f62a556bb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Sep 2024 17:49:56 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/longest_palindrome | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/longest_palindrome b/strings/longest_palindrome index 59d7dac22505..762c707022d7 100644 --- a/strings/longest_palindrome +++ b/strings/longest_palindrome @@ -51,7 +51,7 @@ def benchmark(): result = longest_palindrome(key) end_time = timeit.default_timer() elapsed_time = end_time - start_time - + print(f"{key[:30]:<35} -> Longest Palindrome: {result} (Time: {elapsed_time:.6f} seconds)") assert result == value, f"Test failed for {key}. Expected {value}, but got {result}"