0

The problem is here:

Write a version of a palindrome recognizer that accepts a file name from the user, reads each line, and prints the line to the screen if it is a palindrome.

My code so far:

def palindrome_io(file_, mode):
    stuff = "`~!@#$%^&*()_-=+[]{}\|;:,<.>/?"
    with open(file_, mode) as f:
        for line in f:
            for char in line:
                if char in stuff:
                    line.replace(char, "")
            if line.lower() == line[::-1].lower():
                print True
            else:
                print False
    palindrome_io(r'C:\Users\Brian Gunsel\Desktop\test.txt', 'r+')

My input file (test.txt):

Racecar
paLinDRomE
!Racecar?
Document&

It is returning False for all of them, when it should be returning True, False, True, False.

Will
  • 24,082
  • 14
  • 97
  • 108
Brian Gunsel
  • 111
  • 2
  • 9

3 Answers3

3

Python strings are immutable. You need to affect the string returned by the replace method. Also, line feed character must be stripped. This should work:

def palindrome_io(file_, mode):
    stuff = "`~!@#$%^&*()_-=+[]{}\|;:,<.>/?"
    with open(file_, mode) as f:
        for line in f:
            for char in line:
                if char in stuff:
                    line = line.replace(char, "")
            if line.lower().strip() == line[::-1].lower().strip():
                print True
            else:
                print False
palindrome_io(r'C:\Users\Brian Gunsel\Desktop\test.txt', 'r+')
pie
  • 365
  • 2
  • 9
  • What does strip() with _no_ args do? – Brian Gunsel Feb 28 '16 at 01:51
  • It'll remove leading and trailing \r \n \t and space characters – pie Feb 28 '16 at 02:25
  • @pie Actually, complete list of characters strip() will remove is `["\t", "\n", "\v", "\f", "\r", " "]`. See PY_CTF_SPACE entries in _Py_ctype_table (https://hg.python.org/cpython/file/default/Python/pyctype.c). – pie Feb 28 '16 at 15:05
2

So many loops! :) You don't need to loop through the string character by character; str.translate() does this for you.

Try like this:

def palindrome_io(file_, mode):
    with open(file_, mode) as f:
        for line in f:
            line = line.translate(None, "`~!@#$%^&*()_-=+[]{}\|;:,<.>/?").lower().strip()
            print str(line == line[::-1])

palindrome_io(r'C:\Users\Brian Gunsel\Desktop\test.txt', 'r+')

More directly, the problem is that str.replace() returns the replaced string; it doesn't replace it in-place (Python strings are immutable). You're just discarding the return value. But, nonetheless, it's not Pythonic to iterate over each character in the string in a for loop like that, if it can be avoided.

It also seems like there may have been some newlines/whitespace at the end of the characters, so I added a .strip() to our line assignment to remove any such characters (Windows \r\n's perhaps).

Will
  • 24,082
  • 14
  • 97
  • 108
1

It doesn't work because you're using replace() method in a wrong way. string.replace() method returns a altered copy of original string, as strings in python are immutable. Example:

string = "123/321"
string.replace("/", "")
newstring = string.replace("/", "");
print(string) #prints 123/321
print(newstring) #prints 123321