# Palindrome insertion challenge

Here is my solution to the following Daily Coding Problem challenge

Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible anywhere in the word. If there is more than one palindrome of minimum length that can be made, return the lexicographically earliest one (the first one alphabetically).

Example

Input: race

Output: ecarace

I used a PEP8 checker for this code and the only issue I couldn't fix was the "UPPER_CASE" naming style as I don't understand what I am supposed to do to fix this

import itertools

def find_palindrome(word, new_strings) -> str:
"""Find the first palindromic word of a given string by adding new letters"""
for index in range(len(word)):
new_word = word[:index] + "".join(add_string) + word[index:]
if new_word == new_word[::-1]:
return new_word

if __name__ == "__main__":
word = "r"
if word != word[::-1]:
new_strings = sorted(list(itertools.chain.from_iterable(itertools.permutations(word, n)
for n in range(len(word)+1))))
print(find_palindrome(word, new_strings))
else:
print(word)


EDIT I noticed the find_palindrome function didn't return the first alphabetic palindrome so here is an update for this function

def find_palindrome(word, new_strings) -> str:
"""Find the first palindromic word of a given string by adding new letters"""
list_new_words = []
return (sorted(list_new_words)[0])
for index in range(len(word)+1):
new_word = word[:index] + "".join(add_string) + word[index:]
if new_word == new_word[::-1]:
list_new_words.append(new_word)


EDIT 2: It turns out I had updated the main function too, so here is the update for that:

if __name__ == "__main__":
word = "race"
if word != word[::-1]:
new_strings = sorted(list(itertools.chain.from_iterable(itertools.permutations(word, n)
for n in range(len(word)+1))),key=len)

print(find_palindrome(word, new_strings))
else:
print(word)
$$$$

• Ignore the "UPPER_CASE" warnings, it's because you have code in global scope. If you put it in a function it doesn't complain. – Peilonrayz Jun 8 at 13:53
• @EML - I believe your update doesn't work for the word "race". It prints out "racecar" instead of "ecarace". – Justin Jun 9 at 5:24
• Sorry...I had updated the main function too to set the sorted key = len`. Posted update above – EML Jun 9 at 9:49