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)
-
\$\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– Peilonrayz ♦2019年06月08日 13:53:45 +00:00Commented Jun 8, 2019 at 13:53
1 Answer 1
Documentation
It is great that you added a docstring to the function. However, it could use more details, especially regarding the inputs and the returned value.
Also, add a docstring for the code after the "main" guard.
A description of the new_strings
array would be helpful.
Comments
Since the [::-1]
syntax is a bit cryptic, it would be helpful
to add a comment that it is used to reverse a string.
DRY
The len(add_string)
expression is used a couple times. You could
assign it to a variable:
current_length = len(add_string)
This has the side benefit that you only execute len
once.
Simpler
For return
statements like:
return (sorted(list_new_words)[0])
it is common to omit the parentheses:
return sorted(list_new_words)[0]
Tools
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
When I run pylint
, I get this message, which is similar to yours:
C0103: Constant name "word" doesn't conform to UPPER_CASE naming style (invalid-name)
This is in the code after the "main" guard. This means that the tool considers word
to be a constant instead of a variable because you make only one assignment to it.
To avoid the message, you could change all word
to WORD
, but I don't think
that is necessary in this case. One could easily envision wrapping the code
in a loop for multiple words.
Explore related questions
See similar questions with these tags.