Python Forum
Need to improve a programm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to improve a programm
#1
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')
Reply
#2
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:.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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')
Reply
#4
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)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Any pointers on my programm before I hand it in?(I'm new to python, so go easy on me) blacklight 3 1,923 Jul-07-2020, 01:19 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020