4
\$\begingroup\$

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 add_string in new_strings:
        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"""
    add_string_length = 0
    list_new_words = []
    for add_string in new_strings:
        if len(add_string) > add_string_length and list_new_words:
            return (sorted(list_new_words)[0])
        add_string_length = len(add_string)
        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)
```
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Peilonrayz Jun 8 at 13:53
  • \$\begingroup\$ @EML - I believe your update doesn't work for the word "race". It prints out "racecar" instead of "ecarace". \$\endgroup\$ – Justin Jun 9 at 5:24
  • \$\begingroup\$ Sorry...I had updated the main function too to set the sorted key = len. Posted update above \$\endgroup\$ – EML Jun 9 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.