Discussion:
[Tutor] palindrome using stack and queue
Quiles, Stephanie
2015-08-04 03:55:18 UTC
Permalink
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



_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/l
Alan Gauld
2015-08-04 09:04:30 UTC
Permalink
Post by Quiles, Stephanie
i have to write a palindrome tester using a stack and a queue.
Some teachers seem determined to force students to use basic
data structures rather than use the features of the language
appropriately. I think that's a stupid waste of time but
if you have such a teacher you need to go with it.
Post by Quiles, Stephanie
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
print(my_str,"is a palindrome")
print(my_str, "is not a palindrome")
And that's pretty much the best way to do it and apart from the
call to reversed() you are pretty much using a queue and
stack approach.

But since you need to use the classes you've been given...
Post by Quiles, Stephanie
stack = Stack()
stack2 = Stack()
I've no idea why they have 2 stack instances you only need one!
A stack will return items in the opposite order to which
you put them in.

So push() the characters of your input string onto the stack
in much the same way you created my_str2.

pop() the characters back off the stack and concatenate
them to form rev_str.

Compare rev_str and the input to determine if its a
palindrome, as you already do.

Where does the Queue come in? - I honestly don't know.
You could put the characters in a queue before pushing
them into a stack I suppose. But that would just be for
the sake of using the queue!

Given that the built in Python list already has both
push/pop and queue semantics available the whole exercise
is rather foolish IMHO. But you gotta do it.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...