Python Forum
Need to improve a programm - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Need to improve a programm (/thread-16826.html)



Need to improve a programm - Richard_SS - Mar-16-2019

The program checks if the input text is palindrome. After checking a text like "Rise to vote, sir." it wont find it as palindrome, even if it is. I need to improve a program so that it could ignore punctuation, spaces,case etc. I should specify a tuple "forbidden", where i place such characters, and use the membreship test to determine whether a character should be removed or not.
All my attempts of improving the program using that way crushed with different types of errors. Help me if u can, please.

Here is the initial version of the program
def reverse(text):
    return text[::-1]

def is_palindrome(text):              
    return text == reverse(text)

something = (input('Input something --> '))

if (is_palindrome(something)):
    print('The text is polyndrom')
else:
    print('The text is not polyndrom')



RE: Need to improve a programm - ichabod801 - Mar-16-2019

First of all, you would use the lower method (text.lower()) to remove the problem of lower case letters not matching uppercase letters.

For the rest you don't really need a tuple, you could use a string. But if your teach says use a tuple, use a tuple. Note you could easily define it with a string: forbidden = tuple(' ,.!?')

There's two ways you can do it. First, you can use a for loop to loop through the characters in forbidden, and use the replace method them with nothing in the text (text = text.replace(char, '')). Second you could start with an empty string, loop through the characters in text, and add them to the the empty string if char not in forbidden:.


RE: Need to improve a programm - Richard_SS - Mar-16-2019

(Mar-16-2019, 03:25 PM)ichabod801 Wrote: First of all, you would use the lower method (text.lower()) to remove the problem of lower case letters not matching uppercase letters. For the rest you don't really need a tuple, you could use a string. But if your teach says use a tuple, use a tuple. Note you could easily define it with a string: forbidden = tuple(' ,.!?') There's two ways you can do it. First, you can use a for loop to loop through the characters in forbidden, and use the replace method them with nothing in the text (text = text.replace(char, '')). Second you could start with an empty string, loop through the characters in text, and add them to the the empty string if char not in forbidden:.

I have got your pont, please, check it out what i have now. I guess i did something wrong, cuz it replaces just one character and skips others.
def reverse(text):
    return text[::-1]
def is_palindrome(text):
    forbidden = (",",".",' ')
    for i in forbidden:
        if i in a:
            b = a.replace(i,'')
        
    return text == reverse(b)

something = (input('Input something --> '))
a = something.lower()
print(a)
if (is_palindrome(a)):
    print('The text is polyndrom')
else:
    print('The text is not polyndrom')



RE: Need to improve a programm - ichabod801 - Mar-16-2019

In your is_palindrome function, you should use the text parameter you passed to the function, and you need to save the replacements back into that, to keep the changes:

    for i in forbidden:
        if i in text:
            text = text.replace(i,'')
    return text == reverse(text)



RE: Need to improve a programm - Richard_SS - Mar-17-2019

(Mar-16-2019, 09:19 PM)ichabod801 Wrote: In your is_palindrome function, you should use the text parameter you passed to the function, and you need to save the replacements back into that, to keep the changes:
 for i in forbidden: if i in text: text = text.replace(i,'') return text == reverse(text) 
Got the point, i have understood my mistake, now. Thanks you for helping