0

I have to make a program that tests if an input string is a palindrome. A palindrome is a sequence of words that reads the same as the sequence in reverse, such as the word 'noon'. The program requirements are as follows:

  • You must use a stack
  • The program should ignore all characters other than letters.
  • Input from file
  • Test single words and complete sentences
  • There should be some true false results.

Here is my code so far:

f = open('myTextFile.txt', "r")    
line = f.readline()

while line:    
      print(line)    
      line = f.readline()    
      exclude = set("-")    
      line = ''.join(ch for ch in line if ch not in exclude)    
      exclude = set(",")    
      line = ''.join(ch for ch in line if ch not in exclude)    
      exclude = set(".")    
      line = ''.join(ch for ch in line if ch not in exclude)    
      exclude = set(" ")    
      line = ''.join(ch for ch in line if ch not in exclude)

f.close()

My question is what to do next, I'm kind of lost, I removed all the extra characters, should I put each line into a list to work with them separately? Can you just guide me in the right direction?

Anto
  • 4,265
  • 14
  • 63
  • 113
Roger B
  • 1
  • 1
  • The classic way to do this is to have two pointers and check every pair of characters until they point at the same place or pass each other. You don't need to worry about stripping punctuation; you could just have your `incrementPointer` method skip over non-alpha characters. – gcarvelli Nov 20 '14 at 18:01

1 Answers1

0

I don't know how much this will help you but to check for a palindrome I would do something like that:

# to get rid of spaces to check sentences (maybe repeat for punctuation marks) 
line = line.replace(" ","")  
# create reverse string see http://stackoverflow.com/questions/931092/reverse-a-string-in-python
reverse_line = line[::-1]
# check if it is a palindrome
print line == reverse_line

hope I could help a little

d.a.d.a
  • 1,296
  • 1
  • 12
  • 28