I am trying to write a python program to find the closest palindrome to a word. I can add a letter to any part of the string, remove a letter from any part of the string, or change a letter in any part of a string. I have been looking at using the levenshtein distance to find the minimum number of edits needed between two words in the post Edit Distance in Python. But I am not sure how to programmatically find the palindrome that requires the smallest number of edits. Some examples for what I am trying to achieve:
palindrome('hello') = 'ollo'
#you can remove the h and then turn the e into an o, giving a palindrome in 2 steps
levenshtein('hello',palindrome('hello')) = 2
palindrome('test') = 'tet'
#you can remove the s to get a palindrome
levenshtein('test',palindrome('test')) = 1
palindrome('tart') = 'trart'
#adding an r or removing the r produces a palindrome, but both solutions only require 1 edit so either would be acceptable.
levenshtein('tart',palindrome('tart')) = 1
I was able to use the levenshtein code from the linked post to find the distance between two strings. I need help writing a palindrome() function that takes a string and returns the closest palindrome to that string.