3
\$\begingroup\$

I am new to Python and I would like some comments to the following code. It is for checking if a string is a palindrome (in one or two dimensions).

string_to_check = str(input("Please enter a phrase to check if it is palindrome: "))
string_to_check = string_to_check.upper() #transform the string to uppercase
list_to_check = string_to_check.split()
print ('The string to check is: \"{}" and has {} words'.format(string_to_check , number_of_words))


#two-dimension palindrome check - with two full lists 

reversed_list_to_check = list(map(lambda x:x[::-1], list_to_check[::-1]))



if list_to_check == reversed_list_to_check : 
    two_dimension_palindrome = True
    print ("The string %s is a two-dimension palindrome" %string_to_check)
else :
        #one-dimension palindrome check
    string_to_check = "".join(list_to_check) #string without spaces

    if string_to_check == string_to_check[::-1]:
        one_dimension_palindrome = True
        print ('%s is a one-dimension palindrome' % string_to_check)
    else:
        print ("Your string is not a palindrome")

Is this the Pythonic way?

\$\endgroup\$
1
  • \$\begingroup\$ number_of_words is not defined. You could consider removing punctuation. \$\endgroup\$ Oct 6, 2017 at 17:33

3 Answers 3

1
\$\begingroup\$

I made some changes, not sure this is better though. It is all a matter of opinion. This is not intended to be "the" answer.

I however find this quite interesting. The goal in my opinion should always be to write "easy-to-read" code.

i = input("Please enter a phrase to check if it is palindrome: ")
s = str(i).upper() #transform the string to uppercase
l = s.split()

num_words = len(l)
print ('The string to check is: \"{}" and has {} words'.format(s, num_words))

# Variables to test
two_d_palindrome = False
one_d_palindrome = False

# reverse lists
rev_l = list(map(lambda x:x[::-1], l[::-1]))

#two-dimension palindrome check - with two full lists 
if l == rev_l: 
    two_d_palindrome = True
    print ("The string %s is a two-dimension palindrome" % s)

#one-dimension palindrome check
else:
    s = "".join(l) #string without spaces
    if s== s[::-1]:
        one_d_palindrome = True
        print ('%s is a one-dimension palindrome' % s)

#no match
if not (two_d_palindrome or one_d_palindrome):
    print ("Your string is not a palindrome")
\$\endgroup\$
1
\$\begingroup\$

I find this very interesting. Here’s what my version of it would be like. I’ve added a remove_punctuation(). I often break my code into small chunks. My Python version is 3.6.1.

from string import punctuation

def remove_punctuation(txt):
    return ''.join(ch for ch in txt if ch not in punctuation)

def pal_text(txt):
    return remove_punctuation(txt).lower()

def word_list(txt):
    return pal_text(txt).split()

def word_list_length(words):
    return len(word_list(words))

def reverse_word_list(words):
    return list(map(lambda x: x[::-1], word_list(words)[::-1]))

def is_2D_palindrome(txt):
    return word_list(txt) == reverse_word_list(txt)

def is_1D_palindrome(txt):    
    txt = ''.join(word_list(txt))
    return txt == txt[::-1]


def main():
    phrase = input("Please enter a phrase to check if it is a palindrome: ")
    print('The phrase to check is: "{}" and has {} word(s).'.format(phrase, word_list_length(phrase)))

    if is_2D_palindrome(phrase):
        print ('"{}" is a two-dimension palindrome.'.format(phrase))
    elif is_1D_palindrome(phrase):
        print ('"{}" is a one-dimension palindrome.'.format(phrase))
    else:
        print('"{}" is not a palindrome.'.format(phrase))

if __name__ == '__main__':
    main()

I ran it a couple times and here’s how it works:

Please enter a phrase to check if it is a palindrome: Madam madam
The phrase to check is: "Madam madam" and has 2 word(s).
"Madam madam" is a two-dimensional palindrome.
>>> 

Please enter a phrase to check if it is a palindrome: palindrome
The phrase to check is: "palindrome" and has 1 word(s).
"palindrome" is not a palindrome.
>>>

Please enter a phrase to check if it is a palindrome: Dammit, I'm Mad!
The phrase to check is: "Dammit, I'm Mad!" and has 3 word(s).
"Dammit, I'm Mad!" is a one-dimensional palindrome.

Thanks for sharing this. It was fun :)

\$\endgroup\$
0
\$\begingroup\$

maybe you can use reversed() :

>>> s="saitias"
>>> list(reversed(s))==list(s)
True
>>> s="saperlipopette"
>>> list(reversed(s))==list(s)
False
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.