Created
April 2, 2024 12:34
-
-
Save andersonbosa/a21c5b2eb3b0066e8075a9992b46ba8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NOT_POSSIBLE='not possible' | |
PALINDROME = 'palindrome' | |
MIN_PALINDROME_LENGTH = 3 | |
def is_palindrome(word): | |
return word == word[::-1] | |
def remove_char_at(word, index): | |
return word[:index] + word[index + 1:] | |
def palindrome_creator(string): | |
# rule 1 | |
if is_palindrome(string): | |
return PALINDROME | |
# rule 2 | |
for i in range(len(string)): | |
if len(string) > MIN_PALINDROME_LENGTH and is_palindrome(remove_char_at(string, i)): # remove 1 char | |
return string[i] | |
if len(string) > MIN_PALINDROME_LENGTH + 1: # remove 2 char | |
str_temp = remove_char_at(string, i) | |
for j in range(len(str_temp)): | |
if len(str_temp) > MIN_PALINDROME_LENGTH and is_palindrome(remove_char_at(str_temp, j)): | |
return string[i] + string[i + j + 1] | |
# rule 3 | |
return NOT_POSSIBLE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment