I've searched all over, but Have not been able to find exactly what I need help on.
I am creating a program that takes user input of a word and determines if it is a Palindrome or not....the catcher is I need to use stacks. They are confusing to me. I can write a simple Palindrome program without stacks, but not with. Any advice?
This is what I have so far.

class Stack():
	
	def __init__(self):
		self._items = []

	def push(self,obj):
		self._items.append(obj)

	def pop(self):
		return self._items.pop()

	def peek(self):
		return self._items[-1]

	def isEmpty(self):
		return len(self._items)==0

	def __len__(self):
		return len(self._items)

	def __str__(self):
		return "bottom" +str(self._items)+ "top"

	def reverse(self):
		return self._items.reverse()

and then this is where i am really confused.

from stack import Stack  
stack = Stack()  
stack2 = Stack()  

def checkPalindrome(phrase):    
	word = raw_input("Enter a word or number sequence to check for palidrom: ")
	for ch in phrase:
		if 'A' <=ch,='Z' or 'a' <=ch<='z' or
'16'<=ch<='25':
			stack.push(ch)
	for letters in phrase:
		if 'A' <=ch,='Z' or 'a' <=ch<='z' or
'16'<=ch<='25':
		stack2.push(letters)  
    stack2.reverse() 
#I need to have some type of Yes Palindrome or No Palindrom statement after the program decides if it is or is not a palindrome. I am not sure how to do that with the stacks?
	return result

Recommended Answers

All 6 Replies

I do not understand purpose of ' 1 6 ' < = c h < = ' 2 5 '

Sorry, I cleaned it up the 2nd part a little bit but am still confused on how to implement the stack. Any thoughts or critques to help this newbie... trying to get output that says it either is or isn't a palindrome.

from stack import Stack
	stack1 = Stack()
	stack2 = Stack()
	
	def palindromeChecker(word):  
		word = raw_input("Enter a word to check if it is a Palindrome: ")
		if len(word) <2: return TRUE
                stack.push(ch)  
          if word[0] !=word[-1]: return FALSE 
                stack2.push(letters)  
            stack2.reverse() 
		return palindromeChecker(word[1:-1])

Update: I adjusted some things, but am getting this error message when running the palindromeChecker program.

Traceback (most recent call last):
File "C:\Python27\palindromeChecker", line 7, in <module>
from stack import Stack
ImportError: No module named stack
>>>

I am confused because I had stack.py created and saved. Any advice? Sorry I am very new to python and stacks- pretty obvious I guess.

"""
Program: stack.py
Author: Kali Teel
Create a stack to verify if input is a palindrome
"""
class Stack():  

    def __init__(self):  
        self._items = []  

	def push(self,obj):  
        self._items.append(obj)  
   
    def pop(self):  
		return self._items.pop()  

    def peek(self):  
        return self._items[-1]  

    def isEmpty(self):  
        return len(self._items)==0 

    def __len__(self):  
        return len(self._items)  

    def __str__(self):  
        return "bottom "+str(self._items)+ " top" 

    def reverse(self):  
         return self._items.reverse() 
	
	from stack import Stack
	stack1 = Stack()
	stack2 = Stack()

and then running the Palindrome Checking through

"""
Program: palindromeChecker.py
Author: Kali Teel
Create a program to check if a word is a palindrome using a stack
"""

from stack import Stack

def palindromeChecker(word):  
    word = raw_input("Enter a word to check if it is a Palindrome: ")
    if len(word) <2: return TRUE
    stack.push(letters)
    stack.pop(letters)
    if word[0] !=word[-1]: return FALSE 
    stack2.push(letters)  
    stack.pop(letters)
    stack2.reverse() 
    return palindromeChecker(word[1:-1])

You have not used the Stack class (notice capital S) to create instance of it called stack. Also why have you import in first code as the stack is defined inside the module?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.