[Tutor] palindrome using stack and queue

Quiles, Stephanie stephanie.quiles001 at albright.edu
Tue Aug 4 05:55:18 CEST 2015


Hello ,

 i have to write a palindrome tester using a stack and a queue. You will need to handle strings that may have upper/lower case letters and white space between the letters. We will not include punctuation marks in our strings. Here’s an example: The user inputs otto, you read the string in, you print out something like “otto is a palindrome.” The user inputs r a   DA    r, you output “r a    DA    r is a palindrome.”
 
here iw the code i originally had which worked: 
def main():
    my_str = input("Enter a string: ")
    my_str2 = [c for c in my_str.lower() if c.isalpha()]
    rev_str = reversed(my_str2)
    # check if the string is equal to its reverse
    if list(my_str2) == list(rev_str):
        print(my_str,"is a palindrome")
    else:
        print(my_str, "is not a palindrome")


if __name__ == '__main__':
    main()

But they want us to use a stack and a queue so how would i go about doing that? Here are the stack and queue classes 

class Stack:
    """Top of the stack is at the end of the list"""
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()


stack = Stack()
stack2 = Stack()
class Queue:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def enqueue(self, item):
        self.items.insert(0,item)

    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)


any help is always appreciated

stephanie 





More information about the Tutor mailing list