Skip to content

Instantly share code, notes, and snippets.

@andersonbosa
Created April 2, 2024 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonbosa/a21c5b2eb3b0066e8075a9992b46ba8b to your computer and use it in GitHub Desktop.
Save andersonbosa/a21c5b2eb3b0066e8075a9992b46ba8b to your computer and use it in GitHub Desktop.
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