Practice palindrome

2

I have a question on how to do the extra of the following exercise

Write a palindrome method (phrase) that receives a phrase or word and returns if it is palindrome or not. Then, write a main program that asks the user for a word, sends the word to the previous method and then tells the user if it is palindrome.

Extra: develop a more complex method, in which spaces, periods and commas can be included, but they are not taken into account, and where the characters marked are the same as those not marked. It must work with numbers too.

Example for the extra: I heard about mom: it hurt.

The first part develops it like this:

def palindromo(palabra):
   si_es=1
   x= palabra[::-1]
    if x != palabra:
     si_es=0
   return si_es

def principal():
    palis=str(input("Digite la palabra: "))
    if palindromo(palis):
        print("La palabra %s es un polindromo" %palis)
    else:
        print("La palabra %s no es un polindromo" %palis)
    
asked by monito_19 18.02.2018 в 05:43
source

2 answers

2

A fairly general way to achieve it would be, given the phrase we want to check:

  • Change all accented vowels, or with umlauts by the same vowels without accent and without umlauts, while changing all punctuation marks by spaces.
  • Remove spaces from the resulting chain
  • Pass the result to lowercase.
  • And then check if the result is a palindrome.

    To do each of the steps:

  • The str.translate() function can be helpful. It is a little-known function that requires previously creating a translation dictionary using str.maketrans() . This function receives two chains. In the first one is the list of characters that we want to convert, and in the second one the list of characters already converted (both must be of the same length). See the example below.
  • We split the string into words using space as a separator ( str.split() ) and put everything together again without separations ( str.join() )
  • We use str.lower() .
  • By the way, you can simplify your code by eliminating the intermediate variable si_es and directly returning True or False . With all this the function will remain:

    def palindromo(frase):
        # 1. Quitar acentos y signos de puntuación
        tr = str.maketrans("áéíóúüñÁÉÍÓÚÜÑ.,;!¡¿?", "aeiouunAEIOUUN       ")
        frase = frase.translate(tr)
    
        # 2. Quitar espacios
        frase = "".join(frase.split())
    
        # 3. Pasar todo a minúsculas
        frase =frase.lower()
    
        # Comprobar resultado palindrómico
        if frase == frase[::-1]:
          return True
        else:
          return False
    

    I have verified that using that code correctly detects the following famous palindromes:

      

    The abbot gave rice to the fox.

         

    Do not trace on that cardboard!

         

    Are they mules, or civic students?

         

    I heard about mom: It hurt.

    Or this less known (because I just invented it: -)

      

    No types, be python

        
    answered by 18.02.2018 в 17:56
    0

    The easiest way is to process your word by replacing the characters you are going to ignore and the characters with a tilde with no tilde before checking if it is palindrome ...

    import string
    
    def palindromo(palabra):
       palabra_procesada = palabra
       palabra_procesada = string.replace(palabra_procesada, ",", "")
       palabra_procesada = string.replace(palabra_procesada, ".", "")
       palabra_procesada = string.replace(palabra_procesada, " ", "")
       palabra_procesada = string.replace(palabra_procesada, "á", "a")
       palabra_procesada = string.replace(palabra_procesada, "é", "e")
       palabra_procesada = string.replace(palabra_procesada, "í", "i")
       palabra_procesada = string.replace(palabra_procesada, "ó", "o")
       palabra_procesada = string.replace(palabra_procesada, "ú", "u")
       si_es=1
       x= palabra_procesada[::-1]
        if x != palabra_procesada:
         si_es=0
       return si_es
    
    def principal():
        palis=str(input("Digite la palabra: "))
        if palindromo(palis):
            print("La palabra %s es un polindromo" %palis)
        else:
            print("La palabra %s no es un polindromo" %palis)
    
        
    answered by 18.02.2018 в 17:12