School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,089 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,070 people online right now. Registration is fast and FREE... Join Now!




Palindrome Checker using stacks

 

Palindrome Checker using stacks

brramiz

2 Oct, 2009 - 08:47 PM
Post #1

New D.I.C Head
*

Joined: 14 Apr, 2009
Posts: 11


My Contributions
i'm making a palindrome checker using stacks, what i'm doing in my code is making 2 stacks of the same thing then reversing one of the stacks, where i'm getting stuck is how to then compare the 2 stacks to see if they would be a palindrome or not.

if you can think of maybe a better way to do this (while still using stacks, it's for a class so i have to use stacks) i would love to hear any better ways thank you!!

here is my code:

CODE

from stack import Stack
stack = Stack()
stack2 = Stack()
def palindromeChecker(phrase):
  
    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'<=letters<='Z' or 'a'<=letters<='z' or '16'<=leters<='25':
            stack2.push(letters)
        stack2.reverse()


User is offlineProfile CardPM
+Quote Post


Stutzbach

RE: Palindrome Checker Using Stacks

3 Oct, 2009 - 04:04 AM
Post #2

New D.I.C Head
*

Joined: 6 Jul, 2008
Posts: 29



Thanked: 5 times
My Contributions
"stack" isn't one of the modules included in the Python library. If you want an answer on how to use the module, it would be helpful if you told us where the module came from. biggrin.gif
User is offlineProfile CardPM
+Quote Post

brramiz

RE: Palindrome Checker Using Stacks

3 Oct, 2009 - 06:42 AM
Post #3

New D.I.C Head
*

Joined: 14 Apr, 2009
Posts: 11


My Contributions
ok, i've created "stacks" using lists with the class below that's how i have stacks but i cant use the slice operator to compare them either

CODE

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()


User is offlineProfile CardPM
+Quote Post

Nallo

RE: Palindrome Checker Using Stacks

3 Oct, 2009 - 09:16 AM
Post #4

New D.I.C Head
*

Joined: 19 Jul, 2009
Posts: 24



Thanked: 3 times
My Contributions
Ah, so the problem is how to compare two stacks.

Think of the stacks as 2 decks of cards face down. Lets say you are only allowed to turn the top cards of both decks and put them away afterwards (you are allowed to repeat that). So how do you test if the order of cards in both decks is the same?

You turn the top cards and compare them. If they are unequall then the order of cards is different. If they are equall you put them away and turn the top cards of the remaining decks. You compare them .... Do this untill one deck is empty or you find an unequality.

The problem to compare two stacks is exactly the same.


A side note: I am a bit surprised, the your stack class has a reverse method. Usually stacks dont have a reverse method. Only push, pull, isempty, maybe peek and length.

This post has been edited by Nallo: 3 Oct, 2009 - 09:29 AM
User is offlineProfile CardPM
+Quote Post

programble

RE: Palindrome Checker Using Stacks

12 Oct, 2009 - 02:24 PM
Post #5

D.I.C Regular
Group Icon

Joined: 21 Feb, 2009
Posts: 423



Thanked: 10 times
Dream Kudos: 50
My Contributions
Simply pop a value from each stack and compare them. If all the pairs are equal, it is a palindrome.
User is offlineProfile CardPM
+Quote Post

Tshiknn

RE: Palindrome Checker Using Stacks

18 Oct, 2009 - 05:06 PM
Post #6

New D.I.C Head
*

Joined: 18 Oct, 2009
Posts: 18



Thanked: 2 times
My Contributions
Simple enough.

CODE

def stack_equals(stack1, stack2):
    while True:
        try:
            if stack1.pop() != stack2.pop():
                return False
        except IndexError:
            return True


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 11:20AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month